go_study/bilibili/aceld/var/test.go
2024-11-19 12:27:49 +08:00

61 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
/*
四种声明变量的方式
*/
import (
"fmt"
)
var gA int = 100
var gB = 200
func main() {
// 方法一:声明一个亦是 默认的值是0
var a int
fmt.Println("a = ", a)
fmt.Printf("type of a is %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\ntype of bb is %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\ntype of cc is %T\n", cc, cc)
// 方法四:(常用的方法省略var关键字直接自动匹配:=只能用在函数体内,不能用于声明全局变量
e := 100
fmt.Println("e = ", e)
fmt.Printf("type of e is %T\n", e)
g := 3.14
fmt.Println("g = ", g)
fmt.Printf("type of g is %T\n", g)
// ====
fmt.Println("gA = ", gA, ", gB = ", gB)
// 声明多个变量
var xx, yy int = 100, 200
fmt.Println("xx = ", xx, ", yy = ", yy)
var kk, ll = 100, "Alice"
fmt.Println("kk = ", kk, ", ll = ", ll)
// 多行多变量声明
var (
vv int = 100
jj bool = true
)
fmt.Println("vv = ", vv, ", jj = ", jj)
}