package main
import "fmt"
func main() {
// Declare an array of five strings that is initialized
// to its zero value.
var strings [5]string
strings[0] = "Apple"
strings[1] = "Orange"
strings[2] = "Banana"
strings[3] = "Grape"
strings[4] = "Plum"
// Iterate over the array of strings.
for index, fruit := range strings {
fmt.Println(index, fruit)
}
// Declare an array of 4 integers that is initialized
// with some values.
numbers := [4]int{10, 20, 30, 40}
for i := 0; i < len(numbers); i++ {
fmt.Println(i, numbers[i])
}
}
不同类型
// Sample program to show how arrays of different sizes are
// not of the same type.
// 示例程序演示不同大小的数组。
// 不是同一类型的。
package main
import "fmt"
func main() {
// Declare an array of 5 integers that is initialized
// to its zero value.
var five [5]int
// Declare an array of 4 integers that is initialized
// with some values.
four := [4]int{10, 20, 30, 40}
// Assign one array to the other
five = four
// ./example2.go:21: cannot use four (type [4]int) as type [5]int in assignment
fmt.Println(four)
fmt.Println(five)
}
数组大小决定其寻址能力
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how the behavior of the for range and
// how memory for an array is contiguous.
package main
import "fmt"
func main() {
// Declare an array of 5 strings initialized with values.
five := [5]string{"Annie", "Betty", "Char", "Doug", "Edra"}
// Iterate over the array displaying the value and
// address of each element.
for i, v := range five {
fmt.Printf("value[%s] Address[%p] IndexAddr[%p]
", v, &v, &five[i])
}
/*
数组地址没有改变,但是下标地址一直改变
value[Annie] Address[0xc42000e1d0] IndexAddr[0xc4200840f0]
value[Betty] Address[0xc42000e1d0] IndexAddr[0xc420084100]
value[Char] Address[0xc42000e1d0] IndexAddr[0xc420084110]
value[Doug] Address[0xc42000e1d0] IndexAddr[0xc420084120]
value[Edra] Address[0xc42000e1d0] IndexAddr[0xc420084130]
*/
}
值传递
// Sample program to show how the for range has both value and pointer semantics.
// 示例程序,以显示for range如何具有值和指针语义。
package main
import "fmt"
func main() {
// Using the pointer semantic form of the for range.
five := [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", five[1])
for i := range five {
five[1] = "Jack"
if i == 1 {
fmt.Printf("Aft[%s]
", five[1])
}
}
// Using the value semantic form of the for range.
five = [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", five[1])
for i, v := range five { // 块级别作用域,
five[1] = "Jack"
if i == 1 {
fmt.Printf("v[%s]
", v)
}
}
// Using the value semantic form of the for range but with pointer
// semantic access. DON'T DO THIS.
five = [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", five[1])
for i, v := range &five { // 指针值 最终取到的是值
five[1] = "Jack"
if i == 1 {
fmt.Printf("v[%s]
", v)
}
}
/* 以上示例说明go是传值,值复制(拷贝副本)*/
}
/*
Bfr[Betty] : Aft[Jack]
Bfr[Betty] : v[Betty]
Bfr[Betty] : v[Jack]
*/
练习
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Declare an array of 5 strings with each element initialized to its zero value.
//
// Declare a second array of 5 strings and initialize this array with literal string
// values. Assign the second array to the first and display the results of the first array.
// Display the string value and address of each element.
package main
import (
"fmt"
)
// Add imports.
func main() {
// Declare an array of 5 strings set to its zero value.
var fivestring [5]string
fmt.Printf(fivestring[0])
// Declare an array of 5 strings and pre-populate it with names.
five_s := [5]string{"1s", "2s", "3s", "4s", "5s"}
println(five_s[0])
// Assign the populated array to the array of zero values. 将已填充的数组分配为零值数组。
var name [5]string
name = five_s
println(name[0])
fmt.Println("
")
fmt.Println("
")
// Iterate over the first array declared.
// Display the string value and address of each element.
for i, v := range name {
fmt.Println(v, &name[i])
}
}
/*
1s 0xc4200840f0
2s 0xc420084100
3s 0xc420084110
4s 0xc420084120
5s 0xc420084130
*/