变量定义
var name str = "name"
var name := "name"
name := "name"(不可外部定义)
常量定义
const Pi = 3.1415
数组定义
array := [5]int {1,2,3,4,5}
两种遍历
for i:=0;i<len(str);i++{
fmt.Println(i,str[i]) //byte 遍历
}
for i,ch :=range str{
fmt.Println(i,ch)//字符遍历
}
打印
fmt.Println(name,age)
fmt.Printf("%s,%d",name,page) //格式化打印
fmt.Printf("%T",name,) //打印类型
fmt.Printf("%t",true or false) //格式化打印
类型转换
var num int = 10
var num1 int32 = int32(num)
var num float32=32.99
var num1 = int(num)
const name, dept = "GeeksforGeeks", "CS"
s:= fmt.Sprint(name, " is a ", dept, " Portal.\n") //返回字符串
output GeeksforGeeks is a CS Portal.
var str = "666"
var b int
fmt.Sscanf(a,"%d",&b)
strconv.Itoa //int类型转str
strconv.Itoa //str类型转int
输入输出
fmt.Scanf("%d", &a)
类型别名
type bieming = int64