๐น Go Dictionary
Every keyword, operator, and symbol explained.
package keywordDeclares which package this file belongs to.
package main
import keywordBrings external packages into scope.
import "fmt"
func keywordDeclares a function. Programs start at func main().
func main() {}var keywordDeclares a variable with explicit type.
var x int = 5
const keywordDeclares a constant that cannot be changed.
const Pi = 3.14
struct keywordGroups fields into a named composite type.
type Point struct { X, Y int }interface keywordDefines method signatures a type must implement.
type Stringer interface { String() string }map keywordBuilt-in hash table mapping keys to values.
m := map[string]int{}go keywordStarts a goroutine โ lightweight concurrent function.
go myFunc()
defer keywordSchedules function to run when surrounding function returns.
defer file.Close()
for keywordThe only loop in Go. Can act as while or infinite loop.
for i:=0; i<10; i++ {}range keywordIterates over slice, map, string, or channel.
for i, v := range slice {}if keywordConditional โ runs block if condition is true.
if x > 0 { }switch keywordMulti-way branch. Cases do not fall through by default.
switch x { case 1: }return keywordExits function and optionally returns values.
return x, nil
:= operatorShort variable declaration โ declares and assigns, type inferred.
x := 42
== operatorEquality comparison.
x == y
!= operatorNot-equal comparison.
x != y
&& operatorLogical AND.
a && b
|| operatorLogical OR.
a || b
& operatorAddress-of โ gives a pointer to a variable.
p := &x
* operatorPointer dereference โ accesses value at pointer.
val := *p
make builtinCreates slices, maps, and channels.
make([]int, 10)
len builtinReturns length of string, slice, array, or map.
len(slice)
append builtinAdds elements to end of a slice.
slice = append(slice, 5)
nil keywordZero value for pointers, maps, slices, channels, interfaces.
if err != nil {}error typeBuilt-in interface for errors.
return nil, errors.New("oops")fmt.Println functionPrints values followed by newline.
fmt.Println("Hello")fmt.Printf functionPrints formatted output. %s=string %d=int %v=default.
fmt.Printf("%d", val)fmt.Errorf functionCreates formatted error.
return fmt.Errorf("not found: %s", id)