看代码吧~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package main import ( "fmt" ) type XCDataStu struct { Id int `json:"id" xorm:"id"` Name string `json:"name" xorm:"name"` } type XCDataStu1 struct { Id int `json:"id" xorm:"id"` Str1 string `json:"str1" xorm:"str1"` Db1 string `json:"db1" xorm:"db1"` Device_type string `json:"Device_type" xorm:"Device_type"` DeviceTypeName string `json:"DeviceTypeName"` } type XCDataStuAll struct {//结构体嵌套 XCDataStuinall XCDataStu XCDataStu1inall XCDataStu1 } func main() { /*1*/ xcData := []XCDataStu{ XCDataStu{Id: 758, Name: "David758"}, XCDataStu{Id: 759, Name: "David759"}, } /*2*/ xcdataall := make([]XCDataStuAll, len(xcData)) /*xcdataall[0]={ XCDataStu{Id: 758, Name: "David758"}, XCDataStu1{Id: 0, Str1: "dsa", Db1: "dsa", Device_type: "fhls", DeviceTypeName: "dasf"}}*/ fmt.Println(len(xcData)) fmt.Println(xcData[0]) fmt.Println(xcData[1]) i := 0 for ; i < 2; i++ { xcdataall[i].XCDataStuinall.Id = xcData[i].Id xcdataall[i].XCDataStuinall.Name = xcData[i].Name } fmt.Println(xcdataall) } |
结构体切片:
可以直接声明时初始化,如程序中的1
可以用make,但是必须给定长度,否则不能使用下标进行赋值。
如程序中的2
1
2
3
4
5
|
var xcdataall []XCDataStuAll for ; i < 2; i++ { xcdataall[i].XCDataStuinall.Id = xcData[i].Id xcdataall[i].XCDataStuinall.Name = xcData[i].Name } |
程序编译没错,但是最后运行后会出现panic。。。
panic: runtime error: index out of range
补充:go遍历结构体(struct)字段对应的值,切片(slice),字典(map)
一、遍历结构体字段:
eg1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package main import ( "fmt" "reflect" ) type person struct { name string age int } func main() { v := reflect.ValueOf(person{"steve", 30}) count := v.NumField() for i := 0; i < count; i++ { f := v.Field(i) switch f.Kind() { case reflect.String: fmt.Println(f.String()) case reflect.Int: fmt.Println(f.Int()) } } } |
输出结果:
steve
30
eg2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package main import ( "fmt" "reflect" ) type NotknownType struct { s1, s2, s3 string } var secret interface{} = NotknownType{"Ada", "Go", "Oberon"} func main() { value := reflect.ValueOf(secret) for i := 0; i < value.NumField(); i++ { fmt.Printf("Field %d: %v\n", i, value.Field(i)) } } |
输出结果:
Field 0: Ada
Field 1: Go
Field 2: Oberon
二、遍历切片:
for range 结构
1
2
3
4
5
6
7
8
9
10
|
package main import ( "fmt" ) func main(){ slice := []string{"hello","world","hello","everyone!"} for k,val:=range slice{ fmt.Printf("slice %d is :%s\n",k,val ) } } |
输出结果:
slice 0 is :hello
slice 1 is :world
slice 2 is :hello
slice 3 is :everyone!
三、遍历map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package main import ( "fmt" ) func main() { m := make(map[string]string) m["1"] = "hello" m["2"] = "world" m["3"] = "go" m["4"] = "is" m["5"] = "cool" fmt.Printf("The corresponding relationship between key and value is:\n") for key, val := range m { fmt.Printf("%v===>%v\n", key, val) } } |
输出结果:
The corresponding relationship between key and value is:
1===>hello
2===>world
3===>go
4===>is
5===>cool
但是还有一个问题,上面的程序不做改动运行第二次,结果顺序就会改变,因为map遍历出来结果是无序的,这不好控制,也不利于业务逻辑;当业务依赖key次序时,需要引入“sort”包来解决随机化问题
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package main import ( "fmt" "sort" ) func main() { m := make(map[string]string) m["1"] = "hello" m["2"] = "world" m["3"] = "go" m["4"] = "is" m["5"] = "cool" sorted_keys := make([]string, 0) for k, _ := range m { sorted_keys = append(sorted_keys, k) } sort.Strings(sorted_keys) for _, k := range sorted_keys { fmt.Printf("%v=====>%v\n", k, m[k]) } } |
输出结果是:
1=====>hello
2=====>world
3=====>go
4=====>is
5=====>cool
注意:
输出的结果运行多次不会改变顺序。
但是key的先后顺序是按照字母或者数字排列的。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/m0_37561165/article/details/80481979