这是我参与「第五届青训营 」伴学笔记创作活动的第 1 天
1.简介
1.1 配置环境
跟着官网做就好了,我的机子是mac系统 直接brew install go就行了
1.2 ide
可以选择vscode或者goland
财力雄厚就选第二个
2. 语法
每行不需要分号结尾
创建对象
var 变量名 = 变量值 或者 变量名 := 变量值
可以使用科学记数法来做计算 const i = 3e20 / h
if
bool 表达式不需要括号
但是内部必须使用花括号。
if 7%2 == 0 {
// ...
} else {
// ...
}
switch
不需要break
可以当作 if-elseif 会更清晰
a := 2
switch a {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
case 4, 5:
fmt.Println("four or five")
default:
fmt.Println("other")
}
在 if 和 switch 的 bool语句 中可以有 声明语句 :
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
switch t := time.Now(); t.Hour() < 12 {
case true:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
for
可以使用for { }来死循环
Go没有while
for {
fmt.Println("loop")
}
数组
创建语法var a [5]int
业务中常用切片