The Go Programming Language
Go Programming Blueprints - Second Edition
Go in Action
Go in Practice
Building Microservices with Go
Go: Design Patterns for Real-World Projects
Go: Building Web Applications
reference: The Go Programming Language
// array of 3 integers
// the array literal is: [3]
var a [3]int
var myVar32Bytes [32]byte
var q [3]int = [3]int{1, 2, 3}
q := [...]int{1, 2, 3} // same as before
q := [3]int{1, 2, 3} // same as before
type Currency int
const (
USD Currency = iota
EUR
GBP
RMB
)
symbol := [...]string{USD: "$", EUR: "€", GBP: "£", RMB: "¥"}
fmt.Println(RMB, symbol[RMB]) // "3 ¥"
r := [...]int{99: -1}
It looks like an array type without a size A slice has three components: a pointer, a length, and a capacity
months := [...]string{1: "January", /* ... */, 12: "December"}
Q2 := months[4:7]
summer := months[6:9]
fmt.Println(Q2) // ["April" "May" "June"]
fmt.Println(summer) // ["June" "July" "August"]
make([]T, len)
make([]T, len, cap) // same as make([]T, cap)[:len]
ages := make(map[string]int) // mapping from strings to ints
ages := map[string]int{
"alice": 31,
"charlie": 34,
}
ages := make(map[string]int)
ages["alice"] = 31
ages["charlie"] = 34
ages["alice"] = 32
fmt.Println(ages["alice"]) // "32"
delete(ages, "alice") // remove element ages["alice"]
example: https://play.golang.org/p/n3JdCTj45R
func main() {
var x int
x = 20
fmt.Println(&x) // address of x
p := &x
// The variable to which p points is written *p.
fmt.Println(*p) // p contains of address of x
*p = 2 // equivalent to x = 2
fmt.Println(*p)
fmt.Println(x)
fmt.Println(&x) // address of x
}