funcmain() { a := [...]float64{67.7, 89.8, 21, 78} fmt.Println("length of a is",len(a))
}
运行结果:
1
length of a is 4
您甚至可以忽略声明中数组的长度并将其替换为…让编译器为你找到长度。这是在下面的程序中完成的。
示例代码:
1 2 3 4 5 6 7 8 9 10
package main
import ( "fmt" )
funcmain() { a := [...]int{12, 78, 50} // ... makes the compiler determine the length fmt.Println(a) }
运行结果:
1
[12 78 50]
遍历数组:
1 2 3 4 5 6 7 8 9 10
package main
import"fmt"
funcmain() { a := [...]float64{67.7, 89.8, 21, 78} for i := 0; i < len(a); i++ { //looping from 0 to the length of the array fmt.Printf("%d th element of a is %.2f\n", i, a[i]) } }
运行结果:
1 2 3 4
0 th element of a is 67.70 1 th element of a is 89.80 2 th element of a is 21.00 3 th element of a is 78.00
使用range遍历数组:
1 2 3 4 5 6 7 8 9 10 11 12 13
package main
import"fmt"
funcmain() { a := [...]float64{67.7, 89.8, 21, 78} sum := float64(0) for i, v := range a {//range returns both the index and value fmt.Printf("%d the element of a is %.2f\n", i, v) sum += v } fmt.Println("\nsum of all elements of a",sum) }
运行结果:
1 2 3 4 5 6
0 the element of a is 67.70 1 the element of a is 89.80 2 the element of a is 21.00 3 the element of a is 78.00
sum of all elements of a 256.5
如果您只需要值并希望忽略索引,那么可以通过使用_ blank标识符替换索引来实现这一点。
1 2
for _, v := range a { //ignores index }
多维数组
Go 语言支持多维数组,以下为常用的多维数组声明语法方式:
1
var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
funcmain() { a := [...]string{"USA", "China", "India", "Germany", "France"} b := a // a copy of a is assigned to b b[0] = "Singapore" fmt.Println("a is ", a) fmt.Println("b is ", b) }
运行结果:
1 2
a is [USA China India Germany France] b is [Singapore China India Germany France]
funcmain() { a := [3]int{5, 78, 8} var b [5]int b = a //not possible since [3]int and [5]int are distinct types }
切片(Slice)
什么是切片
Go 语言切片是对数组的抽象。 Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片(“动态数组”),与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大 切片是一种方便、灵活且强大的包装器。切片本身没有任何数据。它们只是对现有数组的引用。 切片与数组相比,不需要设定长度,在[]中不用设定值,相对来说比较自由 从概念上面来说slice像一个结构体,这个结构体包含了三个元素:
funcmain() { numa := [3]int{78, 79 ,80} nums1 := numa[:] //creates a slice which contains all elements of the array nums2 := numa[:] fmt.Println("array before change 1",numa) nums1[0] = 100 fmt.Println("array after modification to slice nums1", numa) nums2[1] = 101 fmt.Println("array after modification to slice nums2", numa) }
运行结果:
1 2 3
array before change 1 [78 79 80] array after modification to slice nums1 [100 79 80] array after modification to slice nums2 [100 101 80]
funcmain() { var countryCapitalMap map[string]string /* 创建集合 */ countryCapitalMap = make(map[string]string) /* map 插入 key-value 对,各个国家对应的首都 */ countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "Rome" countryCapitalMap["Japan"] = "Tokyo" countryCapitalMap["India"] = "New Delhi" /* 使用 key 输出 map 值 */ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } /* 查看元素在集合中是否存在 */ captial, ok := countryCapitalMap["United States"] /* 如果 ok 是 true, 则存在,否则不存在 */ if(ok){ fmt.Println("Capital of United States is", captial) }else { fmt.Println("Capital of United States is not present") } m := map[string]string{"a": "A", "b": "B"} for k, v := range m { fmt.Println(k, v) // b 8; a A } for k := range m { fmt.Println("key", k) // key a; key b }
运行结果:
1 2 3 4 5 6 7 8 9
Capital of France is Paris Capital of Italy is Rome Capital of Japan is Tokyo Capital of India is New Delhi Capital of United States is not present a A b B key a key b
funcmain() { /* 创建 map */ countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"} fmt.Println("原始 map") /* 打印 map */ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } /* 删除元素 */ delete(countryCapitalMap,"France"); fmt.Println("Entry for France is deleted") fmt.Println("删除元素后 map") /* 打印 map */ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } }
运行结果:
1 2 3 4 5 6 7 8 9 10
原始 map Capital of France is Paris Capital of Italy is Rome Capital of Japan is Tokyo Capital of India is New Delhi Entry for France is deleted 删除元素后 map Capital of Italy is Rome Capital of Japan is Tokyo Capital of India is New Delhi
funcmain() { name := "Hello World" fmt.Println(name) }
string的使用
访问字符串中的单个字节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package main
import ( "fmt" )
funcmain() { s := "Hello World" for i := 0; i < len(s); i++ { fmt.Printf("%d ", s[i]) } fmt.Printf("\n") for i := 0; i < len(s); i++ { fmt.Printf("%c ", s[i]) } }
%c:相应Unicode码点所表示的字符
运行结果:
1 2
72 101 108 108 111 32 87 111 114 108 100 H e l l o W o r l d