Cheat Sheet
My personal cheat sheet for golang
Naming Convention
Thanks to: https://medium.com/@kdnotes/golang-naming-rules-and-conventions-8efeecd23b68
Files
Go follows a convention where source files are all lower case with underscore separating multiple words.
Compound file names are separated with _
File names that begin with “.” or “_” are ignored by the go tool
Files with the suffix
_test.goare only compiled and run by thego testtool.
Functions
The case defines if it will be public or private for the package.
Private:
func writeToDb(){}Public:
func WriteToDb(){}
The same for struct properties.
Constants
Constant should use all capital letters and use underscore _ to separate words.
Ex: const CONNECTION_URL := "...."
Variables
Generally, use a relatively simple (short) name.
Consistent naming style should be used the entire source code
user to u
userID to uid
If variable type is
bool, its name should start withHas,Is,CanorAllow, etc.A single letter represents index:
i, j, k
Variables
IF
FOR
SWITCH
FUNCTION
Folders
Read File
Write File
Error Handling
Date and Time
Time Format: https://golang.org/src/time/format.go
Structs
Creating Packages
Pointers
Interfaces
Enum
WaitGroup
Defer
Panic and Recover
Channels and Concurrency
Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.
Youtube:
Unit Test
Last updated
Was this helpful?