๐Ÿน Go Dictionary

Every keyword, operator, and symbol explained.

package keyword
Declares which package this file belongs to.
package main
import keyword
Brings external packages into scope.
import "fmt"
func keyword
Declares a function. Programs start at func main().
func main() {}
var keyword
Declares a variable with explicit type.
var x int = 5
const keyword
Declares a constant that cannot be changed.
const Pi = 3.14
struct keyword
Groups fields into a named composite type.
type Point struct { X, Y int }
interface keyword
Defines method signatures a type must implement.
type Stringer interface { String() string }
map keyword
Built-in hash table mapping keys to values.
m := map[string]int{}
go keyword
Starts a goroutine โ€” lightweight concurrent function.
go myFunc()
defer keyword
Schedules function to run when surrounding function returns.
defer file.Close()
for keyword
The only loop in Go. Can act as while or infinite loop.
for i:=0; i<10; i++ {}
range keyword
Iterates over slice, map, string, or channel.
for i, v := range slice {}
if keyword
Conditional โ€” runs block if condition is true.
if x > 0 { }
switch keyword
Multi-way branch. Cases do not fall through by default.
switch x { case 1: }
return keyword
Exits function and optionally returns values.
return x, nil
:= operator
Short variable declaration โ€” declares and assigns, type inferred.
x := 42
== operator
Equality comparison.
x == y
!= operator
Not-equal comparison.
x != y
&& operator
Logical AND.
a && b
|| operator
Logical OR.
a || b
& operator
Address-of โ€” gives a pointer to a variable.
p := &x
* operator
Pointer dereference โ€” accesses value at pointer.
val := *p
make builtin
Creates slices, maps, and channels.
make([]int, 10)
len builtin
Returns length of string, slice, array, or map.
len(slice)
append builtin
Adds elements to end of a slice.
slice = append(slice, 5)
nil keyword
Zero value for pointers, maps, slices, channels, interfaces.
if err != nil {}
error type
Built-in interface for errors.
return nil, errors.New("oops")
fmt.Println function
Prints values followed by newline.
fmt.Println("Hello")
fmt.Printf function
Prints formatted output. %s=string %d=int %v=default.
fmt.Printf("%d", val)
fmt.Errorf function
Creates formatted error.
return fmt.Errorf("not found: %s", id)