首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
清单首页golang文章详情

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

}

  • 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

从键盘获取值

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")

}

下一篇
举报
领券