這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport ( "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 mainimport ( "fmt")func main() { a := "my is b" b := []byte(a) fmt.Println(string(b[0]))}
[ `go run test.go` | done: 190.573207ms ]
m
package mainimport ( "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 mainimport ( "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 mainimport ( "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