type base conversion
func main() {
var a int = 20
b := 25.5
//类型转换,注意精度丢失
fmt.Print("\n", (a+int(b))/2)
//输出的啊 Ǵ
resilt := string(500)
fmt.Print("\n", resilt)
//bool是不能和任何类型相互转换的
}
constant
func main() {
//常量 只可以使用布尔型/数字型/和字符串
const (
//d 如果上方没有值的话就会报错
a = 10
b //如果未指定类型和赋值就会和上一行的值相同
)
//输出 10 10
fmt.Print(a, b)
const name = "STRING"
//输出 STRING
fmt.Print(name)
//name = "SSDSD" 不可以在执行中给常量赋值
// iota 就是一个常量计数器
const (
//iota 表示0,下面都是自动加1 L = iota=0
L = iota
M = iota
N = iota
)
// 0 1 2
fmt.Print(L, M, N)
//或者
const (
//iota 表示0,下面都是自动加1 L = iota=0
L1 = iota
M1
N1
)
// 0 1 2
fmt.Print(L1, M1, N1)
const (
L4 = "你好"
M5 = iota
N6
)
// 你好 1 2
fmt.Print(L4, M5, N6)
}
loop through the string and print
f := "152这里是程序"
for i, value := range f {
fmt.Printf("%d, %c \n", i, value)
}
iterate over the array
func main() {
//遍历数组
arr := []int{100, 200, 300}
for i, value := range arr {
fmt.Println(i, "=", value)
}
}