数组、切片、映射【我的go学习第六课】

简介: 数组、切片、映射【我的go学习第六课】

1.Arrays (数组)


  • 数组是定长的。
  • 长度不可改变。


初始化

package main
import (
  "fmt"
)
func main() {
  var scores [10]int
  scores[0] = 99
  fmt.Printf("scoers:%d\n", scores)
  socres_english := [4]int{99, 100, 100, 99}
  for index, value := range socres_english {
    fmt.Printf("%d\t %d", index, value)
  }
}
Starting: C:\Users\livingbody\go\bin\dlv.exe dap --check-go-version=false --listen=127.0.0.1:14487 from c:\Users\livingbody\goworkspace\hello
DAP server listening at: 127.0.0.1:14487
Type 'dlv help' for list of commands.
scoers:[99 0 0 0 0 0 0 0 0 0]
0  991   1002  1003  99
Process 14356 has exited with status 0
Detaching
dlv dap (15128) exited with code: 0


2.切片


切片是轻量的包含并表示数组的一部分的结构。


2.1 make创建切片


# 即创建长度10,容量0的切片
score := make([]int, 0, 10)
  • 切片后可以通过append赋值
  • 切片可以重新切片,然后通过索引赋值
  • 切片超过容量会自动增大,自身倍数
package main
import (
  "fmt"
)
func main() {
  var scores [10]int
  scores[0] = 99
  fmt.Printf("scoers:%d\n", scores)
  socres_english := [4]int{99, 100, 100, 99}
  for index, value := range socres_english {
    fmt.Printf("%d\t %d\n", index, value)
  }
  score_math := make([]int, 0, 10)
  score_math = append(score_math, 100)
  fmt.Println(score_math)
  fmt.Println(score_math[0])
  score_math = score_math[0:8]
  score_math[7] = 99
  fmt.Println(score_math)
  c := cap(score_math)
  fmt.Println(c)
  for i := 0; i < 25; i++ {
    score_math = append(score_math, i)
    if cap(score_math) != c {
      c = cap(score_math)
      fmt.Println(c)
    }
  }
}

输出:

Type 'dlv help' for list of commands.
scoers:[99 0 0 0 0 0 0 0 0 0]
0  99
1  100
2  100
3  99
[100]
100
[100 0 0 0 0 0 0 99]
10
20
40
Process 20448 has exited with status 0
Detaching
dlv dap (7204) exited with code: 0


3.映射Map


就好比其他语言中的 hash 表或者字典。它们的工作方式就是:定义键和值,并且可以获取,设置和删除其中的值。

同样通过make方法创建。

package main
import (
  "fmt"
)
func main() {
  lookup := make(map[string]int)
  lookup["goku"] = 9001
  power, exists := lookup["vegeta"]
  fmt.Println(power, exists)
  total := len(lookup)
  fmt.Println(total)
  delete(lookup, "goku")
  fmt.Println(len(lookup))
}
Starting: C:\Users\livingbody\go\bin\dlv.exe dap --check-go-version=false --listen=127.0.0.1:14812 from c:\Users\livingbody\goworkspace\hello
DAP server listening at: 127.0.0.1:14812
Type 'dlv help' for list of commands.
0 false
1
0
Process 924 has exited with status 0
Detaching
dlv dap (700) exited with code: 0

迭代

for key, value := range lookup {
  ...
}

注意:

  • map迭代没有顺序


目录
相关文章
|
1月前
|
Go 索引
Go 语言中同一 slice 上的切片其底层数组是否是同一个
Go 语言中同一 slice 上的切片其底层数组是否是同一个
24 0
|
1天前
|
存储 编译器 Go
Go语言学习12-数据的使用
【5月更文挑战第5天】本篇 Huazie 向大家介绍 Go 语言数据的使用,包含赋值语句、常量与变量、可比性与有序性
31 6
Go语言学习12-数据的使用
|
4天前
|
Java Go
Go语言学习11-数据初始化
【5月更文挑战第3天】本篇带大家通过内建函数 new 和 make 了解Go语言的数据初始化过程
17 1
Go语言学习11-数据初始化
|
5天前
|
JavaScript 前端开发 Go
Go语言的入门学习
【4月更文挑战第7天】Go语言,通常称为Golang,是由Google设计并开发的一种编程语言,它于2009年公开发布。Go的设计团队主要包括Robert Griesemer、Rob Pike和Ken Thompson,这三位都是计算机科学和软件工程领域的杰出人物。
13 1
|
6天前
|
Go
|
12天前
|
Go
|
13天前
|
存储 程序员 Go
【Go语言专栏】Go语言中的切片与映射
【4月更文挑战第30天】Go语言中的切片和映射是两种强大数据结构。切片是动态数组,长度可变,由指针、长度和容量组成,支持索引、切片、追加和复制操作。映射是键值对结构,基于哈希表实现,支持插入、删除、查找和遍历。两者都是Go语言处理数据的利器,理解其内部实现和用法能提升编程效率。
|
19天前
|
Go C++ 容器
【Go语言快速上手(三)】数组, 切片与映射
【Go语言快速上手(三)】数组, 切片与映射
|
25天前
|
存储 安全 Java
Go语言学习10-指针类型
【4月更文挑战第11天】本篇 Huazie 向大家介绍 Go语言的指针类型
14 2
Go语言学习10-指针类型
|
29天前
|
存储 JSON 缓存
Go语言学习9-结构体类型
【4月更文挑战第8天】本篇 Huazie 向大家介绍 Go语言的接口类型
296 8
Go语言学习9-结构体类型