golang-101-hacks(4)——go build”和 go install

羊羽share · · 829 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

整理 GOPATH目录结构,仅展开src源码目录 Let's tidy up theGOPATH directory and only keep Go source code files left over:

tree

.
├── bin
├── pkg
└── src
├── greet
│ └── greet.go
└── hello
└── hello.go

5 directories, 2 files

在源码greet包的greet.go文件中只有一个函数
The greet.go is greet package which just provides one function:
# cat src/greet/greet.go
package greet

import "fmt"

func Greet() {
        fmt.Println("Hello 中国!")
}
虽然hello.go是一个main利用的包,greet可以构建到可执行的二进制文件中:
While hello.go is a main package which takes advantage of greet and can be built into an executable binary:
# cat src/hello/hello.go
package main

import "greet"

func main() {
        greet.Greet()
}
进入src/hello目录 执行go build命令
(1) Enter the src/hello directory, and execute go build command:

pwd

/root/gowork/src/hello

go build

ls

hello hello.go

./hello

Hello 中国!

我们可以看到在当前目录中创建了一个新的hello命令。
We can see a fresh hello command is created in the current directory.
查看 $GOPATH目录
Check the $GOPATH directory:
# tree
.
├── bin
├── pkg
└── src
    ├── greet
    │   └── greet.go
    └── hello
        ├── hello
        └── hello.go

5 directories, 3 files

除了go build,还有另外一个终极命令
Compared before executing go build, there is only a final executable command more.
(2) Clear the $GOPATH directory again:

# tree
.
├── bin
├── pkg
└── src
    ├── greet
    │   └── greet.go
    └── hello
        └── hello.go

5 directories, 2 files

在hello目录下运行go install
Running go install in hello directory:

# pwd
/root/gowork/src/hello
# go install
#

Check the $GOPATH directory now:

tree

.```
├── bin
│ └── hello
├── pkg
│ └── linux_amd64
│ └── greet.a
└── src
├── greet
│ └── greet.go
└── hello
└── hello.go

6 directories, 4 files

不仅在bin目录中生成hello命令,而且在pkg/linux_AMd64中还生成了greet.a。而SRC文件夹没有任何变化,只包含源代码。
Not only the hello command is generated and put into bin directory, but also the greet.a is in the pkg/linux_amd64. While the src folder keeps clean with only source code files in it and unchanged.
(3) There is -i flag in go build command which will install the packages that are dependencies of the target, but won't install the target. Let's check it:
# tree
.
├── bin
├── pkg
└── src
    ├── greet
    │   └── greet.go
    └── hello
        └── hello.go

5 directories, 2 files
Run go build -i under hello directory:
# pwd
#/root/gowork/src/hello
# go build -i
Check $GOPATH now:
# tree
.
├── bin
├── pkg
│   └── linux_amd64
│       └── greet.a
└── src
    ├── greet
    │   └── greet.go
    └── hello
        ├── hello
        └── hello.go
Except a hello command in src/hello directory, a greet.a library is also generated in pkg/linux_amd64 too.
默认情况下,go build以当前目录的名作为编译后的二进制文件的名,要修改二进制文件名,可以使用-o标记
(4) By default, the go build uses the directory's name as the compiled binary's name, to modify it, you can use -o flag:

# pwd
/root/gowork/src/hello
# go build -o he
# ls
he  hello.go
Now, the command is he, not hello.

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:羊羽share

查看原文:golang-101-hacks(4)——go build”和 go install

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

829 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传