怎么样使用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

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

Go语言操作数据库非常的简单,

他也有一个类似JDBC的东西"database/sql"

实现类是"github.com/go-sql-driver/mysql"

使用过JDBC的人应该一看就懂

对日期的处理比较晦涩,没有JAVA流畅:

复制代码代码如下:

package main

import (

"database/sql"

_ "github.com/go-sql-driver/mysql"

"log"

"time"

)

/*

create table t(

id int primary key auto_increment,

name varchar(20) not null,

ts timestamp

)

*/

func insert(db *sql.DB) {

stmt, err := db.Prepare("insert into t(name,ts) values(?,?)")

defer stmt.Close()

if err != nil {

log.Println(err)

return

}

ts, _ := time.Parse("2006-01-02 15:04:05", "2014-08-28 15:04:00")

stmt.Exec("edmond", ts)

}

func main() {

db, err := sql.Open("mysql", "xx:xx@tcp(127.0.0.1:3306)/mvbox?charset=utf8")

if err != nil {

log.Fatalf("Open database error: %s\n", err)

}

defer db.Close()

err = db.Ping()

if err != nil {

log.Fatal(err)

}

1,go的变量声明顺序是:”先写变量名,再写类型名“,此与C/C++的语法孰优孰劣,可见下文解释:

http://blog.golang.org/gos-declaration-syntax

2,go是通过package来组织的(与python类似),只有package名为main的包可以包含main函数,一个可执行程序有且仅有一个main包,通过import关键字来导入其他非main包。

3,可见性规则。go语言中,使用大小写来决定该常量、变量、类型、接口、结构或函数是否可以被外部包含调用。根据约定,函数名首字母小写即为private,函数名首字母大写即为public。

4,go内置关键字(25个均为小写)。

5,函数不用先声明,即可使用。

6,在函数内部可以通过 := 隐士定义变量。(函数外必须显示使用var定义变量)

7,go程序使用UTF-8编码的纯Unicode文本编写。

8,使用big.Int的陷阱:

http://stackoverflow.com/questions/11270547/go-big-int-factorial-with-recursion

9,从技术层面讲,go语言的语句是以分号分隔的,但这些是由编译器自动添加的,不用手动输入,除非需要在同一行中写入多个语句。没有分号及只需少量的逗号和圆括号,使得go语言的程序更容易阅读。

10,go语言只有一个循环结构——for循环。

11,go里的自增运算符只有——“后++”

12,go语言中的slice用法类似python中数组,关于slice的详细用法可见:http://blog.golang.org/go-slices-usage-and-internals

13,函数也是一个值,使用匿名函数返回一个值。

14,函数闭包的使用,闭包是一个匿名函数值,会引用到其外部的变量。