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, )
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的更多相关文章
- 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 ...
- A Tour of Go Slicing slices
---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The ...
- 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 ...
- 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 ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- go官网教程A Tour of Go
http://tour.golang.org/#1 中文版:http://go-tour-cn.appsp0t.com/#4 package main import ( "fmt" ...
- Go structs、slices、maps
[Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A ...
- go tour - Go 入门实验教程
在线实验地址 - 官网 在线实验地址 - 国内 可以将官方教程作为独立程序在本地安装使用,这样无需访问互联网就能运行,且速度更快,因为是在你的机器上构建并运行代码示例. 本地运行此教程的中文版的步骤如 ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
随机推荐
- webkit私有css3属性 -webkit-overflow-scrolling:touch;
-webkit-overflow-scrolling:touch;/*允许独立的滚动区域和触摸回弹*/ 这个属性可以提高滚动的平滑度
- WPS目录制作方法
学校安排我进行电子技术校本教材的后期制作,汇总完全部文字后,需要编辑一个全书目录,进过一番摸索,使用WPS2009圆满完成了此次任务,愿与诸君共享. 1.显示大纲工具栏 打开“视图”——“工具栏”—— ...
- 388A Fox and Box Accumulation
一开始贪心策略想错了! #include<cstdio> #include<algorithm> using namespace std; ]; int main() { in ...
- Java Web 前端高性能优化(二)
一.上文回顾 上回我们主要从图片的合并.压缩等方面介绍前端性能优化问题(详见Java Web 前端高性能优化(一)) 本次我们主要从图像BASE64 编码.GZIP压缩.懒加载与预加载以及 OneAP ...
- Django里,如何更改ADMIN管理后台的显示
今天在慢慢完善管理后台的一些体验, 第一是要扩展默认显示类,就是现在弄的. 第二是要让一些显示框更自然友好,这是下一次要作的. 在各个app的admin.py里,加入相关的MIXIN类 class S ...
- 服务器部署_linuix下 一台nginx 多域名之二
第二个需求: 1. 一台服务器部署了三个应用web1~web3对应三个域名:www.web1.com.www.web2.com.www.web3.com ,方法参照我另外一篇文章 2. 如果用非www ...
- Codeforces 258 Div2
A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R ...
- plsql 高效原则
sql优化是项复杂的工作,不能简单而论,但是在平时书写脚本时的一些细节可以大大提高我们编写代码的效率,提高代码质量. 以下这些规则部分是我的经验,部分是网络资料,整理后在我平时的工作中运用后得到验证的 ...
- python 检测文件编码等
参考:http://my.oschina.net/waterbear/blog/149852 chardet模块,能够实现文本编码的检查, 核心代码: import chardet chardet.d ...
- 亚洲最佳电影TOP100出炉 你看过几部?
亚洲最佳电影TOP100出炉 你看过几部? 在成立20周年之际,釜山国际电影节和釜山电影中心合作的Asian Cinema 100计划邀请亚洲电影领域较为权威的评论人和电影人共同评选出一张『100 ...