Syntax and Basics
Core Go syntax for everyday packages. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Core Go syntax for everyday packages. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
:= declares and assigns in functions.
n := 42
s := "hi"
n // 42
// s == "hi"Uninitialized variables get zero values.
var i int
var b bool
var p *int
i // 0
b // false
p // nilif can declare a scoped variable.
if n := 3; n > 0 {
n // 3
}Iterate slices, maps, strings, channels.
sum := 0
for _, v := range []int{1, 2, 3} {
sum += v
}
sum // 6No automatic fallthrough; cases are expressions.
n := 2
s := ""
switch n {
case 1:
s = "one"
case 2:
s = "two"
default:
s = "other"
}
s // "two"Swap and multi-value assignment.
a, b := 1, 2
a, b = b, a
a // 2
// b == 1Typed or untyped constants.
const Max = 100
Max // 100iota for related constants.
const (
A = iota // 0
B // 1
C // 2
)
B // 1Explicit conversions only.
var f float64 = 3.9
int(f) // 3& address, * dereference.
x := 5
p := &x
*p = 6
x // 6Runs at function return LIFO.
// defer f.Close()
// prints after function bodyRare; prefer structured control.
// goto Done
// Done:Loop control; break can target labels.
n := 0
for i := 0; i < 5; i++ {
if i%2 == 0 { continue }
n += i
}
n // 1+3 = 4new allocates zeroed; make initializes slices/maps/chans.
s := make([]int, 0, 4)
cap(s) // 4
len(s) // 0Discard values with _.
_, err := strconv.Atoi("x")
err != nil // trueStack versions: Go 1.26.x · chi/gin/echo latest (verify at build)
Reviewed by Chris St. John·Last updated Jul 18, 2026