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. Mongodb FAQ 存储(storage)篇

    1.什么是内存映射文件(memory mapped files)? 内存映射文件是操作系统通过调用函数mmap()创建的一个放在内存中的一个数据文件.这种文件可以当做一个从零开始的内存或者数组,你可以 ...

  2. 如何获得iphone设备的剩余空间

    在手机终端开发的时候,我们需要关注手机剩余空间,因为手机不像电脑一样空间宽裕,当设备空间比较少得时候需要释放空间. 用法:先引入头文件 #include <sys/param.h> #in ...

  3. 取得inputStream的长度

    1.网络下载文件 URL url = new URL(strUrl); HttpURLConnection httpconn = (HttpURLConnection)url.openConnecti ...

  4. 浏览器九宫格的简单实现 - 蒋宇捷的专栏 - 博客频道 - CSDN.NET

    CSS3 来源:http://blog.csdn.net/hfahe/article/details/6125890#1536434-hi-1-22083-42d97150898b1af15ddaae ...

  5. ANGULAR 2 FOR REACT DEVELOPERS

    Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...

  6. Burp Suite Walkthrough(英文版)

    Burp Suite is one of the best tools available for web application testing. Its wide variety of featu ...

  7. POJ2200+全排列模拟

    简单. 手动的实现全排列 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<a ...

  8. JavaScript 判断是否为undefined

    if (typeof(reValue) == "undefined") {    alert("undefined"); }

  9. SQL跨表更新

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4384039.html]  前提:两张表要更新的字段.关联字段结构一致 更新库:FJPDI_TZ ...

  10. Android:WebView深入使用

    webView = (WebView) findViewById(R.id.info_detail_webview); WebSettings webSettings = webView.getSet ...