跟 js 不一样,go 的数组是固定的,不过可以通过 slice(切片)来截取或增加数组内容。数组的索引从 0 开始,到长度-1结束,数组长度通过 len 获取。
var a [3]int // 数组声明var b [3]int = [3]int{1, 2, 3} // 数组声明+初始化fmt.printf(%v, %v, %#v\n, len(a), a[0], a)fmt.printf(%v, %v, %#v\n, len(b), b[0], b)// 3, 0, [3]int{0, 0, 0}// 3, 1, [3]int{1, 2, 3}
数组未带初始化时,为该类型默认的零值。
a := [...]int{1, 2, 3} // 简化声明b := [...]int{2: -1}fmt.printf(%v, %v, %#v\n, len(a), a[0], a)fmt.printf(%v, %v, %#v\n, len(b), b[2], b)// 3, 1, [3]int{1, 2, 3}// 3, -1, [3]int{0, 0, -1}
省略号 ...是根据初始值个数来计算的的,可以方便我们修改数据。
slice(切片) slice(切片)代表变长的序列,slice 的语法和数组很像,只是没有固定长度而已。
一个 slice 由三个部分构成:指针、长度和容量,内置的 len和 cap函数分别返回 slice 的长度和容量。
arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}a := arr[:]b := arr[:5]c := arr[5:]fmt.printf(%v, %v, %#v\n, len(arr), cap(arr), arr)fmt.printf(%v, %v, %#v\n, len(a), cap(a), a)fmt.printf(%v, %v, %#v\n, len(b), cap(b), b)fmt.printf(%v, %v, %#v\n, len(c), cap(c), c)// 10, 10, [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}// 10, 10, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}// 5, 10, []int{0, 1, 2, 3, 4}// 5, 5, []int{5, 6, 7, 8, 9}
或者直接创建 slice 类型:
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
注意, []里没写数组大小以及 ...,这是声明一个 slice 类型。
slice 之间不能最 ==比较,唯一合法的比较操作是和nil比较。
arr := []int{1, 2, 3}arr = append(arr, 4, 5)b := []int{6, 7, 8}arr = append(arr, b...)fmt.printf(%v, %v, %#v\n, len(arr), cap(arr), arr)// 8, 12, []int{1, 2, 3, 4, 5, 6, 7, 8}
通过 append添加 slice 元素。
map 它是一个无序的key/value对的集合,就类似 js 的对象字面量一样,只是带了类型。
a := make(map[string]int)a[aa] = 11a[bb] = 22b := map[string]int{ aa: 11, bb: 22,}delete(b, aa)fmt.printf(%#v\n, a)fmt.printf(%#v\n, b)// map[string]int{aa:11, bb:22}// map[string]int{bb:22}
通过 make创建,或直接字面量创建 map,通过 delete删除指定键。
a := map[string]map[string]int{ aa: { aaa: 111, }, bb: { bbb: 222, },}fmt.printf(%v\n, a)// map[aa:map[aaa:111] bb:map[bbb:222]]
嵌套的 map 也还好,就是声明部分比较长,键值的表示跟 js 的几乎一样。
结构体 这个概念是我大学的时候学 c 接触到的概念,之后就再也没接触了。。不过 go 的结构体还是比较简单直观的。
type point struct { x int y int}// 或者type point struct { x, y int}
使用:
a := point{11, 22} // 按值的顺序b := point{y: 11, x: 22} // 通过成员赋值fmt.printf(%+v\n, a)fmt.printf(%+v\n, b)// {x:11 y:22}// {x:22 y:11}
嵌入:
type point struct { x, y int}type circle struct { center point radius int}type wheel struct { circle circle spokes int}
使用的时候要注意,不能直接填值,要跟类型一起填充。
a := wheel{circle{point{11, 22}, 33}, 44}fmt.printf(%+v\n, a)// {circle:{center:{x:11 y:22} radius:33} spokes:44}
json 算是扩展说明了,因为只是 结构体 和 encoding/json包的应用。
结构体声明如下:
type movie struct { title string year int `json:released` color bool `json:color,omitempty` actors []string}
应用如下:
var movies = []movie{ {title: casablanca, year: 1942, color: false, actors: []string{humphrey bogart, ingrid bergman}}, {title: cool hand luke, year: 1967, color: true, actors: []string{paul newman}}, {title: bullitt, year: 1968, color: true, actors: []string{steve mcqueen, jacqueline bisset}},}data, err := json.marshal(movies)if err != nil { log.fatalf(json marshaling failed: %s, err)}fmt.printf(%s\n, data)// [{title:casablanca,released:1942,actors:[humphrey bogart,ingrid bergman]},{title:cool hand luke,released:1967,color:true,actors:[paul newman]},{title:bullitt,released:1968,color:true,actors:[steve mcqueen,jacqueline bisset]}]
json 数据里 year 变成了 released,color 变成了 color,而且 color 为 false 时,json 数据里忽略了这个字段。
这是因为构体成员 tag 所导致的,也就是 json:released和 json:color,omitempty控制的。
文本和html模板 乍一看跟 es6 的字符模板一毛一样。。其实还是有一点点小区别的。
const templ = `{{.totalcount}} issues:{{range .items}}=======================number: {{.number}}user: {{.user.login}}title: {{.title | printf %.64s}}age: {{.createdat | daysago}} days{{end}}`
模板中标签中数据输出是以 .开头的字段,如果不是,那就是命令,例如 range开始 end结束的,就是个循环。
而 html 模板跟 text 模板唯一区别就是实体转义。
小结 复合数据类型,其实出了语法外,都是基本类型的各种组合。明天就是函数部分了,加油。