11-array-slice

command
v0.0.0-...-a7405e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 1 Imported by: 0

README

数组

array 固定数组
先声明再初始化
var arr [5]int
arr[0]=1
arr[1]=3
声明同时初始化
arr1 := [5]int {1, 2, 3}

arr2 := [...]int {1, 2, 3}
slice 动态数组
声明
// 声明字符串切片
var strList []string
strList = []string{"asa","esd"}

numListEmpty := []int{1,2,3}
用 make() 函数构造切片
  • make( []T, size, cap )
    • T:切片的元素类型
    • size:就是为这个类型分配多少个元素
    • cap:预分配的元素数量,这个值设定后不影响 size,只是能提前分配空间,降低多次分配空间造成的性能问题
a := make([]int, 2)
b := make([]int, 2, 10)
append 向切片追加单个元素
var numbers []int
numbers = append(numbers, 5)

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL