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. keil 51警告编译优化

    KeilC51中将工程中没有调用的函数不进行编译的方法 把Target Options中的Device页中选上"Use Extended Linker(LX51)instead of BL5 ...

  2. C++学习笔记37:元编程

    元编程 什么是元编程(metaprogramming) 利用模板可以进行编译期计算(数值计算,型式计算和代码计算)的特点进行程序设计 为什么可以进行元编程? C++是两层语言:执行编译期计算的代码称为 ...

  3. Angular js总结

    之前看过一些angular js的相关技术文档,今天在浏览技术论坛的时候发现有问angular js是做什么用的? 于是有了一个想法,对于自己对angular js 的认知做一个总结. 总结: ang ...

  4. Centos 6.4上面用Shell脚本一键安装mysql 5.6.15

    Centos 6.4上面用Shell脚本一键安装mysql 5.6.15  #!/bin/bash if [ `uname -m` == "x86_64" ];then machi ...

  5. [XJOI NOI2015模拟题13] B 最小公倍数 【找规律】

    题目链接:XJOI - NOI2015-13 - B 题目分析 通过神奇的观察+打表+猜测,有以下规律和性质: 1) 删除的 n 个数就是 1~n. 2) 当 c = 2 时,如果 n + 1 是偶数 ...

  6. Python图形图像处理库的介绍之Image模块

    http://onlypython.group.iteye.com/group/wiki/1372-python-graphics-image-processing-library-introduce ...

  7. QT中的字符串处理函数

    Fn 1 : arg 这个函数的具体声明不写了,它有20个重载,典型的示例代码如下: 1: #include <QtCore/QCoreApplication> 2: #include & ...

  8. char和QChar(Unicode的编码与内存里的值还不是一回事)

    char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’; 那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111, ...

  9. !!对python列表学习整理列表及数组详细介绍

    1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...

  10. Co-variant array conversion from x to y may cause run-time exception

    http://stackoverflow.com/questions/8704332/co-variant-array-conversion-from-x-to-y-may-cause-run-tim ...