A Tour of Go Making slices

Slices are created with the make function. It works by allocating a zeroed array and returning a slice that refers to that array:

a := make([]int, 5)  // len(a)=5

To specify a capacity, pass a third argument to make:

b := make([]int, 0, 5) // len(b)=0, cap(b)=5

b = b[:cap(b)] // len(b)=5, cap(b)=5
b = b[1:]      // len(b)=4, cap(b)=4
package main 

import "fmt"

func main() {
    a := make([]int, 5)
    printSlice("a", a)
    b := make([]int, 0, 5)
    printSlice("b", b)
    c := b[:2]
    printSlice("c", c)
    d := c[2:5]
    printSlice("d", d)
}

func printSlice(s string, x []int) {
    fmt.Printf("%s len=%d cap=%d %v\n",
        s, len(x), cap(x), x)
}
时间: 2024-07-31 14:25:59

A Tour of Go Making slices的相关文章

A Tour of Go Nil slices

The zero value of a slice is nil. A nil slice has a length and capacity of 0. (To learn more about slices, read the Slices: usage and internalsarticle.) package main import "fmt" func main() { var z []int fmt.Println(z, len(z), cap(z)) if z == n

A Tour of Go Slicing slices

---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The expression s[lo:hi] evaluates to a slice of the elements from lo through hi-1, inclusive. Thus s[lo:lo] is empty and s[lo:lo+1] has one element. packa

A Tour of Go Exercise: Slices

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

A Tour of Go Slices

A slice points to an array of values and also includes a length. []T is a slice with elements of type T. package main import "fmt" func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p) for i := 0; i < len(p); i++ { fmt

Go structs、slices、maps

[Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A struct literal denotes a newly allocated struct value by listing the values of its fields. You can list just a subset of fields by using the Name: sy

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca