我把Go语言中的类型声明、值赋值等内容进行了简单的笔记
这是什么?
由于不太方便,我会简短地记录下一些可能常用到的模板以备参考。
package main
import (
"fmt"
)
func main() {
package main
import (
"fmt"
)
func main() {
la1 := make(map[string]string)
la1["go"] = "golang"
la1["rb"] = "ruby"
la1["js"] = "javascript"
fmt.Printf("la1=[%v]\n", la1)
fmt.Println()
la2 := make(map[int]string)
la2[0] = "golang"
la2[1] = "ruby"
la2[2] = "javascript"
fmt.Printf("la2=[%v]\n", la2)
fmt.Println()
la3 := make(map[string][]string)
la3["a"] = append(la3["a"], "golang")
la3["b"] = []string{"topaz", "ruby"}
la3["b"] = append(la3["b"], "garnet")
la3["c"] = append(la3["c"], "javascript")
fmt.Printf("la3=[%v]\n", la3)
fmt.Printf(" >> la3[\"b\"][1]=[%s]\n", la3["b"][1])
fmt.Println()
la4 := make(map[string]map[string][]string)
la4["a"] = make(map[string][]string)
la4["a"]["b"] = append(la4["a"]["b"], "golang")
la4["b"] = make(map[string][]string)
la4["b"]["n"] = append(la4["a"]["b"], "ruby")
fmt.Printf("la4=[%v]\n", la4)
fmt.Printf(" >> la4[\"b\"][\"n\"]=[%v]\n", la4["b"]["n"])
fmt.Printf(" >> la4[\"b\"][\"n\"][1]=[%v]\n", la4["b"]["n"][1])
fmt.Println()
la5 := make(map[string]map[string]string)
la5["a"] = make(map[string]string)
la5["a"]["b"] = "golang"
la5["b"] = make(map[string]string)
la5["b"]["a"] = "javascript"
la5["b"]["d"] = "ruby"
fmt.Printf("la5=[%v]\n", la5)
fmt.Printf(" >> la5[\"b\"][\"d\"]=[%s]\n", la5["b"]["d"])
fmt.Println()
la6 := make(map[int]map[string][]string)
la6[2] = make(map[string][]string)
la6[2]["b"] = []string{"javascript", "scala"}
la6[2]["b"] = append(la6[2]["b"], "golang")
la6[8] = make(map[string][]string)
la6[8]["n"] = append(la6[8]["n"], "ruby")
fmt.Printf("la6=[%v]\n", la6)
fmt.Printf(" >> la6[2][\"b\"]=[%v]\n", la6[2]["b"])
fmt.Printf(" >> la6[8][\"n\"]=[%v]\n", la6[2]["b"])
fmt.Printf(" >> la6[8][\"n\"][0]=[%v]\n", la6[8]["n"][0])
fmt.Println()
la7 := make(map[int]map[int][]string)
la7[2] = make(map[int][]string)
la7[2][3] = []string{"javascript", "scala"}
la7[2][3] = append(la7[2][3], "golang")
la7[8] = make(map[int][]string)
la7[8][5] = []string{"javascript", "ruby"}
la7[8][5] = append(la7[8][5], "node.js")
fmt.Printf("la7=[%v]\n", la7)
fmt.Printf(" >> la7[2][3]=[%v]\n", la7[2][3])
fmt.Printf(" >> la7[2][3][0]=[%v]\n", la7[2][3][0])
fmt.Printf(" >> la7[8][5][2]=[%v]\n", la7[8][5][2])
fmt.Println()
}
$ go run a.go;
la1=[map[rb:ruby js:javascript go:golang]]
la2=[map[0:golang 1:ruby 2:javascript]]
la3=[map[a:[golang] b:[topaz ruby garnet] c:[javascript]]]
>> la3["b"][1]=[ruby]
la4=[map[a:map[b:[golang]] b:map[n:[golang ruby]]]]
>> la4["b"]["n"]=[[golang ruby]]
>> la4["b"]["n"][1]=[ruby]
la5=[map[a:map[b:golang] b:map[a:javascript d:ruby]]]
>> la5["b"]["d"]=[ruby]
la6=[map[8:map[n:[ruby]] 2:map[b:[javascript scala golang]]]]
>> la6[2]["b"]=[[javascript scala golang]]
>> la6[8]["n"]=[[javascript scala golang]]
>> la6[8]["n"][0]=[ruby]
la7=[map[2:map[3:[javascript scala golang]] 8:map[5:[javascript ruby node.js]]]]
>> la7[2][3]=[[javascript scala golang]]
>> la7[2][3][0]=[javascript]
>> la7[8][5][2]=[node.js]
数据结构:结构体
使用数组将结构体整合在一起。
package main
import (
"fmt"
)
func main() {
type Animal struct {
Name string
Age int
}
all_zoo :=[][]Animal{}
zoo := []Animal{}
zoo = append(zoo, Animal{Name:"CAT000", Age:10})
zoo = append(zoo, Animal{Name:"DOG000", Age:4})
zoo = append(zoo, Animal{Name:"Elep000", Age:3})
zoo = append(zoo, Animal{Name:"HORSE000", Age:2})
zoo = append(zoo, Animal{Name:"DUCK000", Age:1})
all_zoo = append(all_zoo, zoo)
all_zoo = append(all_zoo, zoo)
for i:=0; i<len(all_zoo); i++ {
for j:=0; j<len(zoo); j++ {
fmt.Printf("[%d %d : %s %d]\n", i, j, all_zoo[i][j].Name, all_zoo[i][j].Age)
}
}
}
请给出中文的翻译,只需要一种选择:
参考资料
-
- Goで二次元配列の初期化を一行で行う – Qiita
-
- Goで多重連想配列 – Qiita
-
- Go言語の基本 — array と slice の違い – Qiita
- Go言語: いろいろなマップの作り方まとめ – Qiita