golang入门小记
不知道为什么,今天突然来了兴致,我要看看golang。
我忽然想起了苏轼,原来我想起了他的那首《记承天寺夜游》
元丰六年十月十二日夜,解衣欲睡,月色入户,欣然起行。念无与为乐者,遂至承天寺寻张怀民。怀民亦未寝,相与步于中庭。庭下如积水空明,水中藻、荇交横,盖竹柏影也。何夜无月?何处无竹柏?但少闲人如吾两人者耳。
1.初始化
go mod init example.com/hello
go mod tidy
go run .
2.创建一个go module
https://golang.google.cn/doc/tutorial/create-module
In Go, the := operator is a shortcut for declaring and initializing a variable in one line
In Go, code executed as an application must be in a main package.
3.Call your code from another module
从本地目录导入模块的一种方法
go mod edit -replace example.com/greetings=…/greetings
go mod tidy
4.异常处理
That’s common error handling in Go: Return an error as a value so the caller can check for it.
5. Return a random greeting
go lang对大小写是有要求的
小写,只能在本包内访问,大写,则可以被导出
go 的init是自动执行的
go slice
formats :=[]string{
}
go map
messages := make(map[string]string)
Add a test
The go test command executes test functions (whose names begin with Test) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.
Compile and install the application
go build
go install
同一个package名可以有多个文件