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, )
printSlice("a", a)
b := make([]int, , )
printSlice("b", b)
c := b[:]
printSlice("c", c)
d := c[:]
printSlice("d", d)
} func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}

A Tour of Go Making slices的更多相关文章

  1. 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 s ...

  2. A Tour of Go Slicing slices

    ---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The ...

  3. 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 u ...

  4. 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 typ ...

  5. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  6. go官网教程A Tour of Go

    http://tour.golang.org/#1 中文版:http://go-tour-cn.appsp0t.com/#4 package main import ( "fmt" ...

  7. Go structs、slices、maps

    [Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A ...

  8. go tour - Go 入门实验教程

    在线实验地址 - 官网 在线实验地址 - 国内 可以将官方教程作为独立程序在本地安装使用,这样无需访问互联网就能运行,且速度更快,因为是在你的机器上构建并运行代码示例. 本地运行此教程的中文版的步骤如 ...

  9. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

随机推荐

  1. js复制黏贴

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. MVC中——Layout和ViewStart以及页面Index之间的关系

    1._ViewStart.cshtml页面是整个MVC中,必定会加载的,它是在一般普通页面,如Index.cshtml页面之前加载. 2._ViewStart.cshtml初始加载页面中,页首一般会包 ...

  3. HTTP错误代码详细介绍

    HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 40 ...

  4. Vector 的清空

    前两天比赛有一道题,有用到了vector的清空,用的是swap,我一开始还不太清楚,所以去查了下资料,转载一篇关于vector的清空的. vector <int> vecInt; ; i& ...

  5. jQuery 在IE下对表单中input type="file"的属性值清除

    对一个文件域(input type=file)使用了验证后,我们总会希望把文件域中的值给清空了,在IE中,由于安全设置的原因,是不允许更改文件域的值的,接下来为大家介绍一下解决方法 一般来说,在对一个 ...

  6. PDF文件结构

    概述PDF是一种不依赖应用程序软件.硬件和操作系统的文件格式.PDF页包含文本.图形和图像.页面外观由内容流(content stream)描述,内容流包含一些列图形对象(graphics objec ...

  7. 判断微信内置浏览器的UserAgent

    要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...

  8. BZOJ_1618_ [Usaco2008_Nov]_Buying_Hay_购买干草(动态规划,完全背包)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1618 有n种物品,每种物品有价值和重量,可以无限拿.现在要满足价值之和大于等于h,问最小重量. ...

  9. [NYOJ 43] 24 Point game

    24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 There is a game which is called 24 Point game ...

  10. WeiXinMPSDK-微信C# SDK

    微信C# SDK 微信公众号:Senparc.Weixin.MP.dll 微信企业号:Senparc.Weixin.QY.dl 微信开放平台:Senparc.Weixin.Open.dll 本库为.N ...