Cheat Sheet

My personal cheat sheet for golang

Naming Convention

Thanks to: https://medium.com/@kdnotes/golang-naming-rules-and-conventions-8efeecd23b68

Files

  1. Go follows a convention where source files are all lower case with underscore separating multiple words.

  2. Compound file names are separated with _

  3. File names that begin with “.” or “_” are ignored by the go tool

  4. Files with the suffix _test.go are only compiled and run by the go test tool.

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

  1. Generally, use a relatively simple (short) name.

  2. 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 with Has, Is, Can or Allow, 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?