赋值
var a1 int
a1=10
a2:=10
a3,b,c:=10,20,30
var (
z1 int =21
z2 float64 =232.21
)
交换
a:=10
b:=30
a,b = b,a
匿名变量(相当于占位)
a,_,c:=10,20,30//结果为 a=10,c=30
赋值
const a1 = 10
const a2 int = 10
const (
z1 int =10
z2 float64 =10.123123
)
枚举器iota
package main
func main() {
//1.每一行,自动累加1
//2.给常量赋值使用
const (
a = iota
b = iota
c = iota
)
println(a, b, c)
//3.遇到const重置为0
const d = iota
println(d)
//4.同一行不累加
const (
a1 = iota
a2, a3, a4 = iota, iota, iota
)
println(a1, a2, a3, a4)
//5.可以只写一个iota
const (
b1=iota
b2
b3
b4
)
println(b1,b2,b3,b4)
}
var w int
fmt.Scan(&w)
var w1 byte = 'a'
var w2 int = int(w1)
print(w2)
type zhengxing int64
const n1 zhengxing =9
type (
zhengshu int64
fudian float64
char byte
)
基本使用
if a==b {
代码1
} else if a>b {
代码2
} else {
代码3
}
if支持初始化语句
if a1:=10,a2:=30;a1>a2 {
...
}
g:=1
switch g {
case 1:
...
case 2:
...
case 3:
...
}
switch支持初始化语句
switch g:=1;g {
case 1:
...
case 2:
...
case 3:
...
}
switch支持条件语句
g:=10
switch {
case g>10:
...
case g<=10:
...
default:
其他
}
for i := 0; i <10; i++ {
循环体
}
当是一个数时,返回下标
s:="asd"
for i := range s{
println(i,string(s[i]))
}
for i,data := range s{
println(i,string(data))
}