golang 2. 變數
阿新 • • 發佈:2021-06-27
package main /* 四種變數的宣告方式 */ import ( "fmt" ) //宣告全域性變數 方法一、方法二、方法三是可以的 var gA int = 100 var gB = 200 //用方法四來宣告全域性變數 // := 只能夠用在 函式體內來宣告 //gC := 200 func main() { //方法一:宣告一個變數 預設的值是0 var a int fmt.Println("a = ", a) fmt.Printf("type of a = %T\n", a) //方法二:宣告一個變數,初始化一個值 var b int = 100 fmt.Println("b = ", b) fmt.Printf("type of b = %T\n", b) var bb string = "abcd" fmt.Printf("bb = %s, type of bb = %T\n", bb, bb) //方法三:在初始化的時候,可以省去資料型別,通過值自動匹配當前的變數的資料型別 var c = 100 fmt.Println("c = ", c) fmt.Printf("type of c = %T\n", c) var cc = "abcd" fmt.Printf("cc = %s, type of cc = %T\n", cc, cc) //方法四:(常用的方法) 省去var關鍵字,直接自動匹配 e := 100 fmt.Println("e = ", e) fmt.Printf("type of e = %T\n", e) f := "abcd" fmt.Println("f = ", f) fmt.Printf("type of f = %T\n", f) g := 3.14 fmt.Println("g = ", g) fmt.Printf("type of g = %T\n", g) // ===== fmt.Println("gA = ", gA, ", gB = ", gB) //fmt.Println("gC = ", gC) // 宣告多個變數 var xx, yy int = 100, 200 fmt.Println("xx = ", xx, ", yy = ", yy) var kk, ll = 100, "Aceld" fmt.Println("kk = ", kk, ", ll = ", ll) //多行的多變數宣告 var ( vv int = 100 jj bool = true ) fmt.Println("vv = ", vv, ", jj = ", jj) }