RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
go语言翻译中文版 go翻译一下

golang-101-hacks(12)——切片作为函数参数传递

注:本文是对 golang-101-hacks 中文翻译。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的安远网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

在Go语言中,函数参数是值传递。使用slice作为函数参数时,函数获取到的是slice的副本:一个指针,指向底层数组的起始地址,同时带有slice的长度和容量。既然各位熟知数据存储的内存的地址,现在可以对切片数据进行修改。让我们看看下面的例子:

In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now. Let's see the following example:

运行结果如下

由此可见,执行modifyValue函数,切片s的元素发生了变化。尽管modifyValue函数只是操作slice的副本,但是任然改变了切片的数据元素,看另一个例子:

You can see, after running modifyValue function, the content of slice s is changed. Although the modifyValue function just gets a copy of the memory address of slice's underlying array, it is enough!

See another example:

The result is like this:

而这一次,addValue函数并没有修改main函数中的切片s的元素。这是因为它只是操作切片s的副本,而不是切片s本身。所以如果真的想让函数改变切片的内容,可以传递切片的地址:

This time, the addValue function doesn't take effect on the s slice in main function. That's because it just manipulate the copy of the s, not the "real" s.

So if you really want the function to change the content of a slice, you can pass the address of the slice:

运行结果如下

go语言简体中文编码包怎么用

二进制包安装的话:如果安装在了默认位置例如/usr/local/go (Windows系统:c:\Go)这个时候可以不需要设置GOROOT,如果改变了安装的这个默认目录,那么就需要设置GOROOT

GOROOT的概念有点Go安装目录的意思

GOPATH是一个开发环境目录的意思,下面必须包含bin、pkg、src,然后再src下面新建项目就可以了

你在没有设置GOPATH的情况下,也可以把项目建在GOROOT/src下面,其实和GOPATH的概念类似

《Go程序设计语言中文版》pdf下载在线阅读全文,求百度网盘云资源

《Go程序设计语言中文版》百度网盘pdf最新全集下载:

链接:

?pwd=0cii 提取码:0cii

简介:本书由《C程序设计语言》的作者Kernighan和谷歌公司Go团队主管Alan Donovan联袂撰写,是学习Go语言程序设计的指南。本书共13章,主要内容包括:Go的基础知识、基本结构、

基本数据类型、复合数据类型、函数、方法、接口、goroutine、通道、共享变量的并发性、包、go工具、测试、反射等。

本书适合作为计算机相关专业的教材,也可供Go语言爱好者阅读 


分享题目:go语言翻译中文版 go翻译一下
文章位置:http://sczitong.cn/article/ddoosci.html