Types and Structs
Composite types and struct definitions. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Composite types and struct definitions. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Named fields preferred.
type User struct{ ID int; Name string }
u := User{ID: 1, Name: "Ada"}
u.ID // 1Grow slices with append.
s := []int{1, 2}
s = append(s, 3)
s // [1 2 3]Comma-ok idiom for presence.
m := map[string]int{"a": 1}
v, ok := m["a"]
v // 1
// ok == trueArrays have fixed length in the type.
var a [3]int
a[0] = 7
a[0] // 7
// len(a) == 3Alias vs defined type.
type ID = int // alias
type UserID int // distinct
UserID(3) // 3Slicing shares underlying array.
s := []int{0, 1, 2, 3}
s[1:3] // [1 2]Pre-size maps when known.
m := make(map[string]int, 8)
m["x"] = 1
len(m) // 1Remove a key.
m := map[string]int{"a": 1}
delete(m, "a")
len(m) // 0Promotion of embedded fields/methods.
type Point struct{ X, Y int }
type Dot struct{ Point }
d := Dot{Point: Point{1, 2}}
d.X // 1Tags for encoding/json etc.
type T struct {
Name string `json:"name"`
}
// reflect.TypeOf(T{}).Field(0).Tag.Get("json") == "name"copy returns number of elements copied.
dst := make([]int, 2)
n := copy(dst, []int{1, 2, 3})
n // 2
// dst == [1 2]nil and empty both len 0; nil is nil.
var s []int
s == nil // true
len(s) // 0Map keys must be comparable.
m := map[[2]int]string{{1, 2}: "a"}
m[[2]int{1, 2}] // "a"Convert between string and []byte.
b := []byte("hi")
string(b) // "hi"Go auto-dereferences struct pointers.
u := &User{ID: 9}
u.ID // 9Stack versions: Go 1.26.x · chi/gin/echo latest (verify at build)
Reviewed by Chris St. John·Last updated Jul 18, 2026