您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在golang中利用結(jié)構(gòu)體嵌套的切片數(shù)組,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
golang 是Google開(kāi)發(fā)的一種靜態(tài)強(qiáng)類型、編譯型、并發(fā)型,并具有垃圾回收功能的編程語(yǔ)言,其語(yǔ)法與 C語(yǔ)言相近,但并不包括如枚舉、異常處理、繼承、泛型、斷言、虛函數(shù)等功能。
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 {//結(jié)構(gòu)體嵌套 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) }
可以直接聲明時(shí)初始化,如程序中的1
可以用make,但是必須給定長(zhǎng)度,否則不能使用下標(biāo)進(jìn)行賦值。
如程序中的2
var xcdataall []XCDataStuAll for ; i < 2; i++ { xcdataall[i].XCDataStuinall.Id = xcData[i].Id xcdataall[i].XCDataStuinall.Name = xcData[i].Name }
程序編譯沒(méi)錯(cuò),但是最后運(yùn)行后會(huì)出現(xiàn)panic。。。
panic: runtime error: index out of range
補(bǔ)充:go遍歷結(jié)構(gòu)體(struct)字段對(duì)應(yīng)的值,切片(slice),字典(map)
eg1:
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()) } } }
輸出結(jié)果:
steve
30
eg2:
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)) } }
輸出結(jié)果:
Field 0: Ada
Field 1: Go
Field 2: Oberon
for range 結(jié)構(gòu)
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 ) } }
輸出結(jié)果:
slice 0 is :hello
slice 1 is :world
slice 2 is :hello
slice 3 is :everyone!
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) } }
輸出結(jié)果:
The corresponding relationship between key and value is:
1===>hello
2===>world
3===>go
4===>is
5===>cool
但是還有一個(gè)問(wèn)題,上面的程序不做改動(dòng)運(yùn)行第二次,結(jié)果順序就會(huì)改變,因?yàn)閙ap遍歷出來(lái)結(jié)果是無(wú)序的,這不好控制,也不利于業(yè)務(wù)邏輯;當(dāng)業(yè)務(wù)依賴key次序時(shí),需要引入“sort”包來(lái)解決隨機(jī)化問(wèn)題
代碼如下:
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]) } }
輸出結(jié)果是:
1=====>hello
2=====>world
3=====>go
4=====>is
5=====>cool
上述內(nèi)容就是怎么在golang中利用結(jié)構(gòu)體嵌套的切片數(shù)組,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。