资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

go语言函数传数组 go语言 数组

go语言:数组

数组是一个由 固定长度 的 特定类型元素 组成的序列,一个数组可以由零个或多个元素组成。 数组是值类型

成都创新互联专注于企业成都全网营销、网站重做改版、湖滨网站定制设计、自适应品牌网站建设、HTML5建站商城系统网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为湖滨等各大城市提供网站开发制作服务。

数组的每个元素都可以通过索引下标来访问,索引下标的范围是从0开始到数组长度减1的位置,内置函数 len() 可以返回数组中元素的个数。

2.类型的打印,结果的第二种打印方式

3.对元素的修改或者赋值

4.判断数组是否相等:长度、类型

4.数组的地址:连续存储的空间

5.数组的赋值、地址、取值

6.数组的默认值

7.数组的初始化

8.数组的逆置

9.求数组的最大值、最小值、平均值

10.对数组字符串进行连接

11.冒泡排序法的实现

12.数组做函数的参数

13.二维数组:赋值和地址

14.二维数组:打印和输出

15. 指针数组,每一个元素都是地址

17.数组的内存分配

go语言中数组使用的注意事项和细节

1、数组是多个 相同类型 的数据的组合,一个数组一旦声明/定义了,其 长度是固定的,不能动态变化 。

2、var arr []int    这时arr就是一个slice 切片 。

3、数组中的元素可以是任何数据类型,包括值类型和引用类型,但是 不能混用 。

4、数组创建后,如果没有赋值,有默认值如下:

    数值类型数组:    默认值为 0

    字符串数组:       默认值为 ""

    bool数组:           默认值为 false

5、使用数组的步骤:

    (1)声明数组并开辟空间

    (3)给数组各个元素赋值

    (3)使用数组

6、数组的下标是从0开始的。

7、数组下标必须在指定范围内使用,否则报panic:数组越界,比如var arr [5]int的有效下标为0~4.

8、Go的数组属于 值类型 ,在默认情况下是 值传递 ,因此会进行值拷贝。 数组间不会相互影响。

9、如想在其他函数中去修改原来的数组,可以使用 引用传递 (指针方式)。

10、长度是数组类型的一部分,在传递函数参数时,需要考虑数组的长度,看以下案例:

题1:编译错误,因为不能把[3]int类型传递给[]int类型,前者是数组,后者是切片;

题2:编译错误,因为不能把[3]int类型传递给[4]int类型;

题3:编译正确,因为[3]int类型传给[3]int类型合法。

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语言函数如何传递数组变量

按值传递函数参数,是拷贝参数的实际值到函数的形式参数的方法调用。在这种情况下,参数在函数内变化对参数不会有影响。

默认情况下,Go编程语言使用调用通过值的方法来传递参数。在一般情况下,这意味着,在函数内码不能改变用来调用所述函数的参数。考虑函数swap()的定义如下。

代码如下:

/* function definition to swap the values */

func swap(int x, int y) int {

var temp int

temp = x /* save the value of x */

x = y /* put y into x */

y = temp /* put temp into y */

return temp;

}

现在,让我们通过使实际值作为在以下示例调用函数swap():

代码如下:

package main

import "fmt"

func main() {

/* local variable definition */

var a int = 100

var b int = 200

fmt.Printf("Before swap, value of a : %d\n", a )

fmt.Printf("Before swap, value of b : %d\n", b )

/* calling a function to swap the values */

swap(a, b)

fmt.Printf("After swap, value of a : %d\n", a )

fmt.Printf("After swap, value of b : %d\n", b )

}

func swap(x, y int) int {

var temp int

temp = x /* save the value of x */

x = y /* put y into x */

y = temp /* put temp into y */

return temp;

}

让我们把上面的代码放在一个C文件,编译并执行它,它会产生以下结果:

Before swap, value of a :100

Before swap, value of b :200

After swap, value of a :100

After swap, value of b :200

这表明,参数值没有被改变,虽然它们已经在函数内部改变。

通过传递函数参数,即是拷贝参数的地址到形式参数的参考方法调用。在函数内部,地址是访问调用中使用的实际参数。这意味着,对参数的更改会影响传递的参数。

要通过引用传递的值,参数的指针被传递给函数就像任何其他的值。所以,相应的,需要声明函数的参数为指针类型如下面的函数swap(),它的交换两个整型变量的值指向它的参数。

代码如下:

/* function definition to swap the values */

func swap(x *int, y *int) {

var temp int

temp = *x /* save the value at address x */

*x = *y /* put y into x */

*y = temp /* put temp into y */

}

现在,让我们调用函数swap()通过引用作为在下面的示例中传递数值:

代码如下:

package main

import "fmt"

func main() {

/* local variable definition */

var a int = 100

var b int= 200

fmt.Printf("Before swap, value of a : %d\n", a )

fmt.Printf("Before swap, value of b : %d\n", b )

/* calling a function to swap the values.

* a indicates pointer to a ie. address of variable a and

* b indicates pointer to b ie. address of variable b.

*/

swap(a, b)

fmt.Printf("After swap, value of a : %d\n", a )

fmt.Printf("After swap, value of b : %d\n", b )

}

func swap(x *int, y *int) {

var temp int

temp = *x /* save the value at address x */

*x = *y /* put y into x */

*y = temp /* put temp into y */

}

让我们把上面的代码放在一个C文件,编译并执行它,它会产生以下结果:

Before swap, value of a :100

Before swap, value of b :200

After swap, value of a :200

After swap, value of b :100

这表明变化的功能以及不同于通过值调用的外部体现的改变不能反映函数之外。


网站题目:go语言函数传数组 go语言 数组
分享链接:http://cdkjz.cn/article/ddehdjs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220