---
title: "Go命令基础之go build"
author: "cakin24"
date: "2017-08-23T18:56:47Z"
tags: ["Go"]
categories: ["编程语言"]
slug: "go-go-build"
draft: false
lastmod: "2017-08-23T18:56:47Z"
id: 2390885
url: "https://cakin24.iteye.com/blog/2390885"
---

**一 简介**

1、用于编译源码文件或代码包。

2、编译非命令源码文件不会产生任何结果文件。

3、编译命令源码文件会在该命令的执行目录中生成一个可执行文件。

4、执行该命令且不追加任何参数时，它会试图把当前目录作为代码包并编译。

5、执行该命令且以代码包的导入路径作为参数时，该代码包及其依赖会被编译。

加入-a标记后，所有涉及到的代码包都会被重新编译。

不加入-a标记，则只会编译归档文件不是最新的代码包。

6、执行该命令且以若干源码文件作为参数时，只有这些文件会被编译。

 

**二 实战**

01. `[root@localhost ds]# ls`
02. `showds.go`
03. `[root@localhost ds]# go build showds.go`
04. `[root@localhost ds]# ls`
05. `showds showds.go`
06. `[root@localhost ds]#./showds`
07. `/root/goc2p/src/helper/ds:`
08. `showds.go`
09. `showds`
10. `[root@localhost ds]# go run showds.go`
11. `/root/goc2p/src/helper/ds:`
12. `showds.go`
13. `showds`
14. `[root@localhost ds]# cd ../../pkgtool/`
15. `[root@localhost pkgtool]# pwd`
16. `/root/goc2p/src/pkgtool`
17. `[root@localhost pkgtool]# go build`
18. `[root@localhost pkgtool]# ls`
19. `envir.go envir_test.go fpath.go ipath.go pnode.go util.go util_test.go`