go(一)变量
package main import ( "fmt" ) func main() { var a int a = 1 var a1 string a1 = "my is a1" b := 2 b1 := "my is b1" fmt.Println(a, "^^^^", a1) fmt.Println(b, "^^^^", b1) }
[ `go run test.go` | done: 208.870174ms ]
1 ^^^^ my is a1
2 ^^^^ my is b1
接下来是件很有趣的,有了python的列表的影子,但go语言称之为“字节数组”
package main import ( "fmt" ) func main() { a := "my is b" b := []byte(a) fmt.Println(string(b[0])) }
[ `go run test.go` | done: 190.573207ms ]
m
package main import ( "fmt" ) func main() { a := "my is b" + " test" b := `my name is ` fmt.Println(a) fmt.Println(b) }
[ `go run test.go` | done: 197.232301ms ]
my is b test
my
name
is
var是声明全局变量与赋值的
const 常量的定义,
type 一般类型的声明
package main import ( "fmt" ) func main() { var ( name = "wang" age = 12 ) fmt.Print("hello", name, "age is:", age) }
[ `go run hello.go` | done: 201.697722ms ]
hellowangage is:12
类型lingzhii:
当变量被声明某种类型后的默认值,
通常情况下值类型为0
bool为false
string为空字符串
package main import ( "fmt" ) func main() { var a int var b string var c bool fmt.Println(a) fmt.Println(b) fmt.Println(c) }
[ `go run test1.go` | done: 215.061592ms ]
0
false