1、声明和赋值
Full Declaration : var i float32 = 64.
Simple Declaration : k:="46"
Block Declaration :
var (
year int = 2021
month int = 2
date int = 32
)
2、作用域
Package Level:
- 包内
package XXX
- 函数外
func main(){ XXX }
- 小写字母开头
- Full Declaration
Global Level:
- 包内
package XXX
- 函数外
func main(){ XXX }
- 大写字母开头
- Full Declaration
Block Level:
- 函数内
示例:
package main
//package level(have to use full declaration)
//(package level && lowercase first letter) variable
// can be accessed by any file in the same package. (package scope)
var i float64 = 7.65
// I (package level && uppercase first letter) variable
// is globally visible.(global scope)
var I float32 = 73.54
func main() {
//declare & assign 1(full declaration):
//block scope
var j float32 = 64.
}
3、类型转换
数值类型:
var i int = 46
var j float32 = 83.22
j=float32(i)
字符串类型:
m=42
k=string(m)
//*(❌Unicode coder)
fmt.Println(k)
//42(✔) Integer To ASCII
k= strconv.Itoa(m)
fmt.Println(k)
4、控制台输出
使用fmt.Printf("%v")
可以输出值
使用fmt.Printf("%T")
可以输出类型
var j float32 = 64.
k := "44"
fmt.Println("hello,go", j, k)
fmt.Printf("%v,%T\n", j, j)
fmt.Printf("%v,%T\n", k, k)
5、常见错误
- 重复声明
- 声明但未使用