Day06(作用域与数组)
作用域与数组
变量作用域
局部变量
函数内定义的变量
1
2
3
4
5
6
7
8
9
10
package main
import (
"fmt"
)
func main() {
var a, b, c = 1, 2, 3
fmt.Println(a, b, c)
}
全局变量
函数外定义的变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
import "fmt"
// 定义全局变量
var h int
func main() {
var a, b int
a = 1
b = 2
h = a + b
fmt.Println(h)
}
形式参数
函数定义中的变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main
import "fmt"
// 声明全局变量
var a = 20
func main() {
// main 函数中声明局部变量
var a = 10
var b = 20
var c = 0
fmt.Printf("main()函数中 a = %d\n", a)
c = sum(a, b)
fmt.Printf("main()函数中 c = %d\n", c)
}
// 函数定义-两数相加
func sum(a, b int) int {
fmt.Printf("sum() 函数中 a = %d\n", a)
fmt.Printf("sum() 函数中 b = %d\n", b)
return a + b
}
1
2
3
4
main()函数中 a = 10
sum() 函数中 a = 10
sum() 函数中 b = 20
main()函数中 c = 30
数组
数组是
Go
语言中一个数据结构数组元素可以通过索引(位置)来读取(或者修改),索引从 0 开始,第一个元素索引为 0,第二个索引为 1,以此类推
数组声明、初始化、访问元素
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 (
"github.com/Cc360428/HelpPackage/UtilsHelp/logs"
)
func main() {
var array = [2]int{1, 2}
logs.Info(array)
var array1 = [...]int{1, 2, 2, 3, 4, 4, 3, 23, 6, 6, 2}
logs.Info(array1)
var n [10]int // n 是一个长度为 10 的数组
var i, j int
// 为数组 n 初始化元素
for i = 0; i < 10; i++ {
n[i] = i + 100 /* 设置元素为 i + 100 */
}
// 输出每个数组元素的值
for j = 0; j < 10; j++ {
logs.Info("Element[%d] = %d ", j, n[j])
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2020/04/07 23:26:07.885 [I] [array.go:10] [1 2]
2020/04/07 23:26:07.886 [I] [array.go:12] [1 2 2 3 4 4 3 23 6 6 2]
2020/04/07 23:26:07.886 [I] [array.go:23] Element[0] = 100
2020/04/07 23:26:07.886 [I] [array.go:23] Element[1] = 101
2020/04/07 23:26:07.886 [I] [array.go:23] Element[2] = 102
2020/04/07 23:26:07.886 [I] [array.go:23] Element[3] = 103
2020/04/07 23:26:07.886 [I] [array.go:23] Element[4] = 104
2020/04/07 23:26:07.886 [I] [array.go:23] Element[5] = 105
2020/04/07 23:26:07.886 [I] [array.go:23] Element[6] = 106
2020/04/07 23:26:07.886 [I] [array.go:23] Element[7] = 107
2020/04/07 23:26:07.886 [I] [array.go:23] Element[8] = 108
2020/04/07 23:26:07.886 [I] [array.go:23] Element[9] = 109
多维数组、向数组传递数组
多维数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import (
"github.com/Cc360428/HelpPackage/UtilsHelp/logs"
)
func main() {
// 数组 - 5 行 2 列
var a = [5][2]int{ {0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
var i, j int
// 输出数组元素
for i = 0; i < 5; i++ {
for j = 0; j < 2; j++ {
logs.Info("a[%d][%d] = %d", i, j, a[i][j])
}
}
}
1
2
3
4
5
6
7
8
9
10
2020/04/07 23:28:47.448 [I] [array2.go:15] a[0][0] = 0
2020/04/07 23:28:47.449 [I] [array2.go:15] a[0][1] = 0
2020/04/07 23:28:47.449 [I] [array2.go:15] a[1][0] = 1
2020/04/07 23:28:47.449 [I] [array2.go:15] a[1][1] = 2
2020/04/07 23:28:47.449 [I] [array2.go:15] a[2][0] = 2
2020/04/07 23:28:47.449 [I] [array2.go:15] a[2][1] = 4
2020/04/07 23:28:47.449 [I] [array2.go:15] a[3][0] = 3
2020/04/07 23:28:47.449 [I] [array2.go:15] a[3][1] = 6
2020/04/07 23:28:47.449 [I] [array2.go:15] a[4][0] = 4
2020/04/07 23:28:47.449 [I] [array2.go:15] a[4][1] = 8
向数组传递数组
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
package main
import (
"github.com/Cc360428/HelpPackage/UtilsHelp/logs"
)
func main() {
// 数组长度为 5
var balance = [5]int{1000, 2, 3, 17, 50}
var avg float32
// 数组作为参数传递给函数
avg = getAverage(balance, 5)
// 输出返回的平均值
logs.Info("平均值为: %f ", avg)
}
func getAverage(arr [5]int, size int) float32 {
var i, sum int
var avg float32
for i = 0; i < size; i++ {
sum += arr[i]
}
avg = float32(sum) / float32(size)
return avg
}
1
2020/04/07 23:31:06.475 [I] [array3.go:16] 平均值为: 214.399994
This post is licensed under
CC BY 4.0
by the author.