go語音基礎之變數和常量
阿新 • • 發佈:2018-12-30
1、多個變數
示例1:
package main import "fmt" func main() { //不同型別變數的宣告(定義) var a int var b float64 a, b = 10, 3.14 fmt.Println("a = ", a) fmt.Println("b = ", b) }
#執行結果
a = 10 b = 3.14
示例2:
package main import "fmt" func main() { //不同型別變數的宣告(定義) var ( a int b float64 ) a, b = 10, 3.14 fmt.Println("a = ", a) fmt.Println("b = ", b) }
#執行結果
a = 10 b = 3.14
示例3:
package main import "fmt" func main() { //可以自動推導型別 var ( a = 1 b = 2.0 ) a, b = 10, 3.14 fmt.Println("a = ", a) fmt.Println("b = ", b) }
#執行結果
a = 10 b = 3.14
2、常量
示例1:
package main import "fmt" func main() { const i int = 10 const j float64 = 3.14 fmt.Println("i = ", i) fmt.Println("j = ", j) }
#執行結果
i = 10 j = 3.14
示例2
package main import "fmt" func main() { const ( i int = 10 j float64 = 3.14 ) fmt.Println("i = ", i) fmt.Println("j = ", j) }
#執行結果
i = 10 j = 3.14
示例3
package main import "fmt" func main() {
//自動推導型別 const ( i = 10 j = 3.14 ) fmt.Println("i = ", i) fmt.Println("j = ", j) }
#執行結果
i = 10 j = 3.14