forked from jincheng9/go-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_array.go
56 lines (45 loc) · 1.26 KB
/
func_array.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import "fmt"
import "reflect"
func sum(array [5]int, size int) int{
sum := 0
for i:=0; i<size; i++ {
sum += array[i]
}
return sum
}
func sumSlice(array []int, size int) int{
sum := 0
for i:=0; i<size; i++ {
sum += array[i]
}
return sum
}
func changeArray(array [3]int) {
array[0] = 10
}
func changeArray2(array *[3]int) {
array[0] = 10
}
func changeArray3(array []int) {
array[0] = 10
}
func main() {
a := [5]int {1, 2, 3, 4, 5} // a := [...]int{1, 2, 3, 4, 5}也可以去调用sum,编译器会自动推导出a的长度5
fmt.Println("type of a:", reflect.TypeOf(a)) // 输出type of a: []int,是一个slice类型
ans := sum(a, 5)
fmt.Println("a length=", len(a), " ans=", ans)
b := []int{1, 2, 3, 4, 5}
ans2 := sumSlice(b, 5)
fmt.Println("b length=", len(b), " ans2=", ans2)
c := [2][3]int{{1,2,3}, {3,4,5}}
fmt.Println("c length=", len(c))
param := [3]int{1,2,3}
changeArray(param)
fmt.Println("param=", param) // param= [1 2 3]
changeArray2(¶m)
fmt.Println("param=", param) // param= [10 2 3]
sliceArray := []int{1,2,3}
changeArray3(sliceArray)
fmt.Println("sliceArray=", sliceArray) // sliceArray= [10 2 3]
}