ABCDEFGHIJKLMNOPQRSTUVWXYZ
1
Data typeZero valuenil 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])DeclarationInitialization%v value in fmt
2
1. Basic data types
3
numbers (int,int8,int16,int32,int64, uint,uint8,uint16,uint32,uint64, byte, float32, float64)0NoYesNovar x intx := 1010
4
string“”NoYesNovar s strings := “go”go
5
boolfalseNoYesNovar b boolb := truetrue
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 initializationNoYes
(when array type is the comparable type)
Novar a [3]inta := [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 initializationNoYes
(when all fields are comparable)
Notype 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
YesNo
(only legal comparison is with nil)
Yesvar a []inta := []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
YesNo
(only legal comparison is with nil)
Yesvar m map[string]intm := 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
pointernilYesYes
(point to the same variable or both are nil)
Yesvar p *intp := &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
channilYesYes
(reference to the same channel data structure or nil)
Yesvar c chan intc := make(chan int)
for unbuffered channel
or
c := make(chan int, 5)
for buffered channel
<nil>
or
0xc42001e180
(address of the channel)
15
funcnilYesNo
(only legal comparison is with nil)
Yes [3]var f func(int) intf := 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)
nilYesNo
(only legal comparison is with nil)
[4]
Novar 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 usevar 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 usevar 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