import "fmt"
var arr [2]int //申明一个数组
func main() {
arr[0] = 1 //数组赋值
fmt.Println(arr)
arrtest := [3]int{1, 2, 3} //数组的另一种申明方式
fmt.Println(arrtest)
a := [...]int{1, 2} //[...]自动识别数组的长度
fmt.Println(a)
fmt.Println(len(a))//输出数组的长度
}
下边是slice的申明和使用其实这就是一种动态的数组
复制代码 代码如下:
package main
import "fmt"
func main() {
d := []int{1, 2, 3} //申明一个slice这个是动态的数组,没有长度
fmt.Println(d)
var q, w []int
q = d[0:1] //可以定取得上边的长度
w = d[1:3]
d = append(d, 2) //向其中添加元素
fmt.Println(d)
fmt.Println(q, w)
}
下边是slice的申明和使用其实这就是一种动态的数组复制代码 代码如下:package main
import "fmt"func main() {d := []int{1, 2, 3} //申明一个slice这个是动态的数组,没有长fmt.Println(d)
var q, w []intq = d[0:1] //可以定取得上边的长度w = d[1:3]d = append(d, 2) //向其中添加元素fmt.Println(d)fmt.Printlnw。
Go语言是谷歌2009年发布的第二款开源编程语言。Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。北京时间2010年1月10日,Go语言摘得了TIOBE公布的2009年年度大奖。
在谷歌公开发布的所有网络应用中,均没有使用Go,但是谷歌已经使用该语言开发了几个内部项目。派克表示,Go是否会对谷歌即将推出的Chrome OS产生影响,还言之尚早,不过Go的确可以和Native Client配合使用。他表示“Go可以让应用完美的运行在浏览器内。”例如,使用Go可以更高效的实现Wave,无论是在前端还是后台。
Go 同时具有两种编译器,一种是建立在GCC基础上的Gccgo,另外一种是分别针对64位x64和32位x86计算机的一套编译器(6g和8g)。谷歌目前正在研发其对ARM芯片和Android设备的支持。
Google对Go寄予厚望。其设计是让软件充分发挥多核心处理器同步多工的优点,并可解决面向对象程序设计的麻烦。它具有现代的程序语言特色,如垃圾回收,帮助程序设计师处理琐碎但重要的内存管理问题。Go的速度也非常快,几乎和C或C++程序一样快,且能够快速制作程序。
按值传递函数参数,是拷贝参数的实际值到函数的形式参数的方法调用。在这种情况下,参数在函数内变化对参数不会有影响。默认情况下,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
这表明变化的功能以及不同于通过值调用的外部体现的改变不能反映函数之外。