[3][4]int
[3]
on the left in the above type literal indicates that this type is an array type. The whole right part following the [4]int
is another array type, which is the element type of the first array type. The element type of the element type (an array type) of the first array type is built-in type int
. The first array type can be viewed as a two-dimensional array type.
package main
import (
"fmt"
)
func main() {
matrix := [3][4]int{
{1, 0, 0, 1},
{0, 1, 0, 1},
{0, 0, 1, 1},
}
matrix[1][1] = 3
a := matrix[1] // type of a is [4]int
fmt.Println(a) // [0 3 0 1]
}
[][]string
is a slice type whose element type is another slice type []string
.
**bool
is a pointer type whose base type is another pointer type *bool
.
chan chan int
is a channel type whose element type is another channel type chan int
.
map[int]map[int]string
is a map type whose element type is another map type map[int]string
. The key types of the two map types are both int
.
func(int32) func(int32)
is a function type whose only return result type is another function type func(int32)
. The two function types both have only one input parameter with type int32
.
chan *[16]byte
chan
keyword at the left most indicates this type is a channel type. The whole right part *[16]byte
, which is a pointer type, denotes the element type of this channel type. The base type of the pointer type is [16]byte
, which is an array type. The element type of the array type is byte
.
package main
import (
"fmt"
"time"
"crypto/rand"
)
func main() {
c := make(chan *[16]byte)
go func() {
// Use two arrays to avoid data races.
var dataA, dataB = new([16]byte), new([16]byte)
for {
_, err := rand.Read(dataA[:])
if err != nil {
close(c)
} else {
c <- dataA
dataA, dataB = dataB, dataA
}
}
}()
for data := range c {
fmt.Println((*data)[:])
time.Sleep(time.Second / 2)
}
}
map[string][]func(int) int
is a map type. The key type of this map type is string
. The remaining right part []func(int) int
denotes the element type of the map type. The []
indicates the element type is a slice type, whose element type is a function type func(int) int
.
package main
import "fmt"
func main() {
addone := func(x int) int {return x + 1}
square := func(x int) int {return x * x}
double := func(x int) int {return x + x}
transforms := map[string][]func(int) int {
"inc,inc,inc": {addone, addone, addone},
"sqr,inc,dbl": {square, addone, double},
"dbl,sqr,sqr": {double, double, square},
}
for _, n := range []int{2, 3, 5, 7} {
fmt.Println(">>>", n)
for name, transfers := range transforms {
result := n
for _, xfer := range transfers {
result = xfer(result)
}
fmt.Printf(" %v: %v \n", name, result)
}
}
}
[]map[struct {
a int
b struct {
x string
y bool
}
}]interface {
Build([]byte, struct {x string; y bool}) error
Update(dt float64)
Destroy()
}
[]
at the left most indicates this type is a slice type. The following map
keyword shows the element type of the slice type is a map type. The struct type denoted by the struct literal enclosed in the []
following the map
keyword is the key type of the map type. The element type of the map type is an interface type which specifies three methods. The key type, a struct type, has two fields, one field a
is of int
type, and the other field b
is of another struct type struct {x string; y bool}
.
T
declared in the following code and the just explained type above denote the identical type.
type B = struct {
x string
y bool
}
type K = struct {
a int
b B
}
type E = interface {
Build([]byte, B) error
Update(dt float64)
Destroy()
}
type T = []map[K]E
len
function can be used to get the length of values of arrays, slices, maps, strings and channels. Generally, the functions in the unsafe
standard package are also viewed as built-in functions. The built-in generic functions have been introduced in previous articles,
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
reflect
standard package.sync
standard package.sync/atomic
standard package.