Go (Golang) Basics Cheatsheet

Quick reference for Go syntax, types, functions, structs, methods, concurrency, and utilities.

ItemDescriptionExampleCategory
packageDefine packagepackage mainSyntax
importImport packagesimport "fmt"Syntax
func main()Program entry pointfunc main() { fmt.Println("Hello, Go") }Syntax
varDeclare variablevar x int = 10Types
:=Short variable declarationx := 10Types
constDefine constantconst pi = 3.14Types
int, float64, string, boolBasic typesvar name string = "Go"Types
array, slice, mapCollectionsnums := []int{1,2,3}Types
funcDefine functionfunc add(a int, b int) int { return a+b }Functions
deferExecute at function exitdefer fmt.Println("Done")Functions
returnReturn valuereturn x + yFunctions
structDefine structtype Person struct { Name string; Age int }Structs
newCreate new structp := new(Person)Structs
p := Person{}Struct literalp := Person{Name: "John", Age: 30}Structs
func (p Person) Method()Define method on structfunc (p Person) Greet() { fmt.Println("Hello", p.Name) }Methods
goStart goroutinego doSomething()Concurrency
channelCommunication between goroutinesch := make(chan int)Concurrency
<-chReceive from channelvalue := <-chConcurrency
ch <- valueSend to channelch <- 10Concurrency
fmt.PrintlnPrint to consolefmt.Println("Hello")Utilities
len()Get length of string, slice, maplen(nums)Utilities
make()Create slice, map, channels := make([]int, 10)Utilities