1、数组
package main
import "fmt"
func main() {
var teacherNameArray = [3]string{
"张宇", "张二", "张三"}
fmt.Println(teacherNameArray)
teacherAgeArray := [3]int{
18, 19, 20}
fmt.Println(teacherAgeArray)
fmt.Println("第一位老师的名字是:", teacherNameArray[0])
fmt.Println("第一位老师的年龄是:", teacherAgeArray[0])
teacherNameArray[2] = "dotbalo"
fmt.Println("修改后的数据:", teacherNameArray)
fmt.Println("数组的长度是:", len(teacherNameArray))
for i := 0; i < len(teacherNameArray); i++ {
fmt.Printf("第%d个数据为: %s\n", i+1, teacherNameArray[i])
}
for k, v := range teacherAgeArray {
fmt.Printf("第%d位老师的年龄是: %d\n", k+1, v)
}
array3 := [...]int{
1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println("array3的长度是:", len(array3))
}
- 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
2、切片
2.1 切片基础使用
package main
import "fmt"
func main() {
var s1 []int
fmt.Println("最初的切片数据:", s1)
fmt.Println("切片的默认长度是:", len(s1))
fmt.Println("切片的默认容量是:", cap(s1))
s1 = append(s1, 7275, 85266)
fmt.Println("长度是:", len(s1))
fmt.Println("容量是:", cap(s1))
fmt.Println("数据:", s1)
s2 := make([]int, 5, 10)
fmt.Println("切片的默认长度是:", len(s2))
fmt.Println("切片的默认容量是:", cap(s2))
fmt.Println("最初的切片数据:", s2)
s2 = append(s2, 1, 2, 3, 4, 5, 6)
fmt.Println("最初的切片数据:", s2)
fmt.Println("长度是:", len(s2))
fmt.Println("容量是:", cap(s2))
s2[0] = 88
fmt.Println("切片的数据:", s2)
for k, v := range s2 {
fmt.Printf("第%d个数据是: %d\n", k+1, v)
}
- 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
- 57
- 58
- 59
- 60
- 61
2.2 切片的元素截取和删除
package main
import "fmt"
func main