| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Data type | Zero value | nil assignable? (and so also could be compared against nil) | Comparable? == (can be used as the key in the map) | Reference Type? (pass by “reference” [1]) | Declaration | Initialization | %v value in fmt | ||||||||||||||||||
2 | 1. Basic data types | |||||||||||||||||||||||||
3 | numbers (int,int8,int16,int32,int64, uint,uint8,uint16,uint32,uint64, byte, float32, float64) | 0 | No | Yes | No | var x int | x := 10 | 10 | ||||||||||||||||||
4 | string | “” | No | Yes | No | var s string | s := “go” | go | ||||||||||||||||||
5 | bool | false | No | Yes | No | var b bool | b := true | true | ||||||||||||||||||
6 | 2. Composite types | |||||||||||||||||||||||||
7 | 2.1. Aggregate types | |||||||||||||||||||||||||
8 | array (fixed size) | Zero value of all it elements (len() returns the size of the array declared). So can be used immediately after declaration without prior initialization | No | Yes (when array type is the comparable type) | No | var a [3]int | a := [3]int{10, 20, 30} or a := [...]int{10, 20, 30} or a := [...]{99: -1} (which will create the array of 100 elements, with last one initialized to -1, while the rest set to their zero value, 0 in the case of the int-s) [2] | [10 20 30] or [0 0 0] for zero value | ||||||||||||||||||
9 | struct (fixed size) | Zero value of all it fields. So can be used immediately after declaration without prior initialization | No | Yes (when all fields are comparable) | No | type point struct { x, y int label string visited bool } var p point | p := point{10, 20, “p”, true} or partially initialize only relevant fields p := point{y: 20, visited: true} or the special case when the struct is part of the map (and so the type can be omitted), i.e.: m := map[string]point{ "p": {10, 20, "p", true}, } | {10 20 p true} or {0 0 false} for zero value | ||||||||||||||||||
10 | 2.2. Reference types | |||||||||||||||||||||||||
11 | slice (dynamic) | nil (len() returns 0). Must be initialized before the use | Yes | No (only legal comparison is with nil) | Yes | var a []int | a := []int{10, 20, 30} or a := make([]int, 0, 10) If the slice is initialized with make and the length is set, i.e. a := make([]int, 3, 10) then len(a) will return that length, i.e. 3 | [10, 20, 30] or [] for zero value (even though the zero value is nil) or [0 0 0] when length was provided for the make | ||||||||||||||||||
12 | map (dynamic) (the key must be comparable using ==) | nil (len() returns 0). Must be initialized before the use | Yes | No (only legal comparison is with nil) | Yes | var m map[string]int | m := make(map[string]int) or m := map[string]int{} or m := map[string]int{“a”: 1, “b”: 2} | []map (for empty or nil map) or map[a: 1 b: 2] | ||||||||||||||||||
13 | pointer | nil | Yes | Yes (point to the same variable or both are nil) | Yes | var p *int | p := &x or p := new(int) (although rarely used, also in this form new() creates the new address, so those pointers are always different, i.e. == returns false) | <nil> or 0xc420074068 (address pointer points to) | ||||||||||||||||||
14 | chan | nil | Yes | Yes (reference to the same channel data structure or nil) | Yes | var c chan int | c := make(chan int) for unbuffered channel or c := make(chan int, 5) for buffered channel | <nil> or 0xc42001e180 (address of the channel) | ||||||||||||||||||
15 | func | nil | Yes | No (only legal comparison is with nil) | Yes [3] | var f func(int) int | f := func(x int) int { return x + 1 } for function value or normally declared function func inc(x int) int { return x + 1 } f := inc | <nil> or 0x48b500 (address of the defined function) | ||||||||||||||||||
16 | 3. Interface type | |||||||||||||||||||||||||
17 | interface{} (abstract type) | nil | Yes | No (only legal comparison is with nil) [4] | No | var any interface{} | any := 5 or any := false or any := “string” And etc. as you can assign value of any type to the interface{} type | <nil> or the %v of the underlying type | ||||||||||||||||||
18 | 4. Bonus | |||||||||||||||||||||||||
19 | bytes.Buffer (is actually a struct, so everything about struct applies here) | Empty buffer ready to use | var b bytes.Buffer A Buffer needs no initialization, ready to use | |||||||||||||||||||||||
20 | strings.Builder (is actually a struct, so everything about struct applies here) (added in Go 1.10) | Empty builder ready to use | var b strings.Builder A Builder needs no initialization, ready to use | |||||||||||||||||||||||
21 | ||||||||||||||||||||||||||
22 | [1] Variables of these types are passed by value, as everything in the Go, although because they are “reference” types, which mean they hold the pointer (reference) to the underlying data structure, so it means that the pointer gets copied (i.e. alias is created), which allows to make the modifications to the underlying data structure pointer points (references) to. | |||||||||||||||||||||||||
23 | ||||||||||||||||||||||||||
24 | [2] Another interesting example (taken from The Go Programming Language book): type Currency int const ( USD Currency = iota EUR GBP RMB) symbol := [...]string{USD: “$”, EUR: “€”, “GBP”: “£”, “RMB”: “¥”} | |||||||||||||||||||||||||
25 | ||||||||||||||||||||||||||
26 | [3] It said that the func is the reference type, and mentions it is possible to affect the original function, although it is not clear how. Any ideas? | |||||||||||||||||||||||||
27 | ||||||||||||||||||||||||||
28 | [4] Although it is possible to compare interfaces with each other, it might fail at runtime when interface type (dynamic types are the same) represents the non comparable type, then it fails with panic. So compare interface values only if you are certain they contain dynamic values of comparable types. | |||||||||||||||||||||||||
29 | ||||||||||||||||||||||||||
30 | ||||||||||||||||||||||||||
31 | ||||||||||||||||||||||||||
32 | ||||||||||||||||||||||||||
33 | ||||||||||||||||||||||||||
34 | ||||||||||||||||||||||||||
35 | ||||||||||||||||||||||||||
36 | ||||||||||||||||||||||||||
37 | ||||||||||||||||||||||||||
38 | ||||||||||||||||||||||||||
39 | ||||||||||||||||||||||||||
40 | ||||||||||||||||||||||||||
41 | ||||||||||||||||||||||||||
42 | ||||||||||||||||||||||||||
43 | ||||||||||||||||||||||||||
44 | ||||||||||||||||||||||||||
45 | ||||||||||||||||||||||||||
46 | ||||||||||||||||||||||||||
47 | ||||||||||||||||||||||||||
48 | ||||||||||||||||||||||||||
49 | ||||||||||||||||||||||||||
50 | ||||||||||||||||||||||||||
51 | ||||||||||||||||||||||||||
52 | ||||||||||||||||||||||||||
53 | ||||||||||||||||||||||||||
54 | ||||||||||||||||||||||||||
55 | ||||||||||||||||||||||||||
56 | ||||||||||||||||||||||||||
57 | ||||||||||||||||||||||||||
58 | ||||||||||||||||||||||||||
59 | ||||||||||||||||||||||||||
60 | ||||||||||||||||||||||||||
61 | ||||||||||||||||||||||||||
62 | ||||||||||||||||||||||||||
63 | ||||||||||||||||||||||||||
64 | ||||||||||||||||||||||||||
65 | ||||||||||||||||||||||||||
66 | ||||||||||||||||||||||||||
67 | ||||||||||||||||||||||||||
68 | ||||||||||||||||||||||||||
69 | ||||||||||||||||||||||||||
70 | ||||||||||||||||||||||||||
71 | ||||||||||||||||||||||||||
72 | ||||||||||||||||||||||||||
73 | ||||||||||||||||||||||||||
74 | ||||||||||||||||||||||||||
75 | ||||||||||||||||||||||||||
76 | ||||||||||||||||||||||||||
77 | ||||||||||||||||||||||||||
78 | ||||||||||||||||||||||||||
79 | ||||||||||||||||||||||||||
80 | ||||||||||||||||||||||||||
81 | ||||||||||||||||||||||||||
82 | ||||||||||||||||||||||||||
83 | ||||||||||||||||||||||||||
84 | ||||||||||||||||||||||||||
85 | ||||||||||||||||||||||||||
86 | ||||||||||||||||||||||||||
87 | ||||||||||||||||||||||||||
88 | ||||||||||||||||||||||||||
89 | ||||||||||||||||||||||||||
90 | ||||||||||||||||||||||||||
91 | ||||||||||||||||||||||||||
92 | ||||||||||||||||||||||||||
93 | ||||||||||||||||||||||||||
94 | ||||||||||||||||||||||||||
95 | ||||||||||||||||||||||||||
96 | ||||||||||||||||||||||||||
97 | ||||||||||||||||||||||||||
98 | ||||||||||||||||||||||||||
99 | ||||||||||||||||||||||||||
100 | ||||||||||||||||||||||||||