be used in place of a var with implicit type func main() { var i, j int = 1, 2 var a int a = 3 b := a // type inference // print 1 2 3 3 fmt.Println(i, j, a, b) }
is a view into the elements of an array func main() { // primes is an array primes := [6]int{2, 3, 5, 7, 11, 13} // s is a slice var s []int = primes[1:4] fmt.Println(s) // 3, 5, 7 }
of elements it contains • cap(s) ◦ the number of elements in the underlying array, counting from the first element in the slice s := []int{2, 3, 4}// len=3, cap=3 s = s[:0] // [], len=0, cap=3 s = s[:2] // [2,3], len=2, cap=3 s = s[1:] // [3], len=1, cap=2
is a function with a special receiver argument type N struct { Num int } // (n N) is the receiver func (n N) Dec() int { return n.Num - 1 } func main() { var num = N{Num:2} fmt.Println(num.Dec()) // 1 }
can send and receive values with the channel operator <- func hello(s string, c chan string) { c <- "hello " + s // send s to channel c } func main() { c := make(chan string) go hello("world", c) greeting := <-c // receive from c fmt.Println(greeting) // hello world }