golang学习笔记——基础知识(1)
观看B站李文周老师的视频学习golang整理的笔记
变量
var 变量名 变量类型
var(
a int
b int
)
a := 10
a,_,c := 1,2,3
//其中2将不会被赋值
type 别名 类型
//type cjp int32 需要再main函数外面为类型定义别名
常亮
const 常量名 = 值
const{
a = 10
b = 20
}
const{
a = iota // a = 0
b = iota // b = 1
}
//当 iota 遇到const时 被重置为0
const a = iota //a = 0
const b = iota //b = 0
字符串
str := "hello world"
fmt.printf("str length is %d" , len(str)) //str length is 11
str := "hello world"
fmt.printf("str[0] is %c",str[0]) //str[0] is h
从键盘获取值
fmt.Scan(&a)
条件语句
-
switch语句【 switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码,fallthrough不会判断下一条case的expr结果是否为true】
var a int = 2
switch {
case a == 2:
fmt.Print("aaaaaa")
fallthrough
case a < 1:
fmt.Print("bbbbbb")
fallthrough
case a == 3:
fmt.Print("cccccc")
}
//输出 aaaaaabbbbbbcccccc
for i := 10; i < 20; i++ {
fmt.Print("asdasa")
}
//使用range关键字
str := "hello world"
for k, v := range str {
fmt.Printf("str[%d] is %c \n", k, v)
}
if a := 10;a < 20{
fmt.Println("a的值小于20")
}