go mod init github.com/用户名/模块名 // 在模块目录下执行此命令创建模块的描述文件
go get github.com/88250/lute@master //更新模块到最新
go
package test // 声明当前代码属于 test 包
import "math/rand"
rand.Int()
// Version 大写字符开头的变量将在 test 包内共享,否则仅在当前文件共享
var Version = "1.0"
控制流
go
package main
import "fmt"
func main() {
x := 27
if x%2 == 0 { // 小括号可以省略
fmt.Println(x, "is even")
} else if num < 10 {
fmt.Println(num, "has only one digit")
} else {
fmt.Println(num, "has multiple digits")
}
// 这里声明的 num 变量仅在这里的全部 if 分支中可用,if 块外不可
if num := givemeanumber(); num < 0 {
fmt.Println(num, "is negative")
}
switch i {// 这里的 i 可以不传
case 0:
fmt.Print("zero...")
fallthrough // 继续下一个 case ,其他语言一般默认继续,go 默认 break
case 1:
fmt.Print("one...")
case 2, 3: // 支持多个表达式
fmt.Print("two...")
case i == 4: // 这里可以直接使用表达式
fmt.Print("four...")
default:
fmt.Print("no match...")
}
for i := 1; i <= 100; i++ {
fmt.Println(i)
}
for x!= 5 { // 当 x == 5 时才会停止
x= rand.Int63n(15)
if num%5 == 0 {
continue
}
fmt.Println(x)
}
for { // 一直循环
fmt.Println(x)
break
}
}