golang学习笔记——基础知识(1)
观看B站李文周老师的视频学习golang整理的笔记
变量
- 
定义
var 变量名 变量类型
- 
多个
var(
    a  int
    b  int
)
- 
自动识别变量类型运算符“:=”
a := 10
- 
匿名变量符“_”
a,_,c := 1,2,3 //其中2将不会被赋值
- 
定义类型别名
type 新类型 类型  //type cjp int32  定义的是一种新的类型 在不强制类型转换的情况下 cjp 和 int32 类型的变量是不能比较的
type 别名 = 类型  //type cjp = int32  起的一个类型别名实际上是同一种类型,在不强制类型转换的情况下 cjp 和 int32 类型的变量是可以比较的
常亮
- 
定义
const 常量名 = 值
- 
定义多个常亮
const(
    a = 10
    b = 20
)
- 
iota的使用
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
     不能使用 str[0] 直接赋值,应先转为[]byte再赋值
从键盘获取值
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语句
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语句
if a := 10;a < 20{
    fmt.Println("a的值小于20")
}

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号