go 代码组织
go 程序组成package, 一个package是一组在相同目录的源代码共同编译而成。在同一个包里,函数、类型、变量、和常量是彼此可见的。一系列相关的包组成了module。一个repo包含一个或者多个module。
GOPATH & GOBIN
go build生成bin文件,如果GOBIN未设置,则安装到GOPATH。如果设置了GOBIN,则安装到GOBIN。
% mkdir first-go-module
% cd first-go-module
% # init a module, module path example/user/hello
% go mod init example/user/hello
go: creating new go.mod: module example/user/hello
% ls -al
total 8
drwxr-xr-x 3 carawang staff 96 Aug 19 21:17 .
drwxr-xr-x 3 carawang staff 96 Aug 19 21:15 ..
-rw-r--r-- 1 carawang staff 37 Aug 19 21:17 go.mod
% # module name has been set.
% cat go.mod
module example/user/hello
go 1.21.5
% mkdir -p example/user/hello
% vi example/user/hello/hello.go
% cat example/user/hello/hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world.")
}
% # go install = go build + install bin 文件到GOPATH/GOBIN
% go install example/user/hello/hello.go
% # 如果GOBIN没有设置,则安装到G

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



