Goproxy 中国 中国最可靠的 Go 模块代理。
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
查看golang版本
go version
注释
/*
块
注释
*/
// 行注释
每个 Go 程序都是由包(package)组成的。
程序入口是 package main
惯例: 包名与导入路径的最后一个目录一致
打包导入:
import(
"fmt"
"math"
)
包被导入后,可用导出的名称调用它
首字母大写的名称是被导出的
类型在变量名之后
多个参数类型一致时,可省略前面的
返回值可以被命名
func bmi(weight, height float64) (bmi float64, is_normal bool) {
bmi = weight / (math.Pow(height, 2))
if (18.5 < bmi) && (bmi < 23.9) {
is_normal = true
}
return
}
var
语句可以定一个变量列表:=
,替代var
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // uint8 的别名
rune // int32 的别名
// 代表一个Unicode码
float32 float64
complex64 complex128
package main
import (
"fmt"
"math/cmplx"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, z, z)
}
使用go build test.go
编译
run go mod init test
main.go run