go语言函数如何传递数组变量

Python012

go语言函数如何传递数组变量,第1张

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

默认情况下,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

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

类型 在变量名后边

也可不显式声明类型, 类型推断, 但是是静态语言, name一开始放字符串就不能再赋值数字

方法,属性 分开 方法名首字母大写就是就是外部可调的

面向对象设计的一个重要原则:“优先使用组合而不是继承”

Dog 也是Animal , 要复用Animal 的属性和方法,

只需要在结构体 type 里面写 Animal

入口也是main, 用用试试

多态, 有这个方法就是这个接口的实现, 具体的类 不需要知道自己实现了什么接口,

使用: 在一个函数调用之前加上关键字go 就启动了一个goroutine

创建一个goroutine,它会被加入到一个全局的运行队列当中,

调度器 会把他们分配给某个 逻辑处理器 的队列,

一个逻辑处理器 绑定到一个 操作系统线程 ,在上面运行goroutine,

如果goroutine需要读写文件, 阻塞 ,就脱离逻辑处理器 直接 goroutine - 系统线程 绑定

编译成同名.exe 来执行, 不通过虚拟机, 直接是机器码, 和C 一样, 所以非常快

但是也有自动垃圾回收,每个exe文件当中已经包含了一个类似于虚拟机的runtime,进行goroutine的调度

默认是静态链接的,那个exe会把运行时所需要的所有东西都加进去,这样就可以把exe复制到任何地方去运行了, 因此 生成的 .exe 文件非常大

主要看优先级别,->比++的优先级别高,++p->num,因为->优先级别高,所以先算p->num,然后num++,p++->num,先算p->num,然后p++