GO速成篇
7views
var a int = 1
var b int
c := 3 // 只能用在函数内部,无法定义全局变量
var d = 4
fmt.Println(a, b, c, d)
const length int = 10
const (
BEIJING = iota
SHANGHAI
SHENZHEN
)
fmt.Println(BEIJING, SHANGHAI, SHENZHEN)
const (
a, b = iota + 1, iota + 2 // iota = 0, a = 1, b = 2
c, d // iota = 1, c = 2, d = 3
e, f // iota = 2, e = 3, f = 4
g, h = iota * 2, iota * 3 // iota = 3, g = 6, h = 9
i, j // iota = 4, i = 8, j = 12
)
func fool(a int, b int) int {
return a + b
}
func Fool(a int, b int) int {
return a + b
}
func fool3(a string, b int) (r1 int, r2 int) {
r1 = 2000
r2 = 3000
return
}
func fool4(a int, b int) (r1, r2 int, r3 string) {
r1 = a + b
r2 = a - b
r3 = "hello"
return
}
func main() {
foox := func() {
fmt.Println("foo2")
}
fool(d, e)
foox()
}
import (
. "./lib2"
_ "./lib3"
lib4 "./libFourTooLongLibName"
)
package main
import (
"fmt"
)
func changeValue(a *int) {
*a = 100
}
func main() {
a := 10
changeValue(&a)
fmt.Println("a = ", a)
}
&a
相当于获取a的地址。
a *int
在内存中存储的是a的地址,非变量值,相当于a = &a
。
*a = 100
相当于找到a存储的内存地址其对应的内存地址,将其赋值100
, *a
就是一个指针,有指向另一个的作用。