GOPATH & GOBIN & GO modules

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值