go语言基础之变量和常量
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