@aliasliyu4
2017-02-14T15:47:12.000000Z
字数 5558
阅读 8914
build != complie
-a 构建一个项目意味着更多比编译你的代码
-b 静态分析,lint检查,安装,配置,打包......
项目的各个阶段都有相同的工具
-a Makefiles作为胶水的角色连接 起go编译链(build, install, clean)
-b 同时也可以使用第三方工具(go lint, go vet, gb, 等等......)
不确定语言
-a 有可能你的项目可能不止依赖仅仅一种语言
-b 生成配置文件,指导页,固定数据等等......
可以在多个平台上使用
-a 用户可以选择任何他想用的系统
-b 夸平台编译
code to build
package main
import "fmt"
func main() {
// Probably the most awesome piece of code you've ever seen.
fmt.Println("i love you!")
}
Makefile
default:
go build
Run
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:24:29]
→ ls -al
total 16
drwxr-xr-x 4 aliasliyu4 staff 136 Feb 14 13:24 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 21 Feb 14 13:23 Makefile
-rw-r--r-- 1 aliasliyu4 staff 134 Feb 14 13:23 main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:24:31]
→ make
go build
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:24:32]
→ ls
Makefile gomake main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:24:34]
→ ./gomake
i love you!
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:24:36]
→
编译: 输入:"make"
你也可以编译多个项目在单个Makefile中。
-a 一个Makefile可以调用另一个Makefile
想要在你的代码中使用更多复杂的工具?
-a 构建一个项目的所有脚本和数据都属于它本身
-b 如果有你不想持续集成的部分,只需要改变你的Makefile
安装: 输入:"make install"
清理: 输入:"make clean"
Improved Make file
# Default target: builds the project
build:
go build
# Installs our project: copies binaries
install:
go install
# Cleans our project: deletes binaries
clean:
go clean
Run
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:51:49]
→ ls -al
total 16
drwxr-xr-x 4 aliasliyu4 staff 136 Feb 14 13:51 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 175 Feb 14 13:50 Makefile
-rw-r--r-- 1 aliasliyu4 staff 134 Feb 14 13:23 main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:51:53]
→ make
go build
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:51:57]
→ ls
Makefile gomake main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:52:05]
→ ./gomake
i love you!
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:52:14]
→ make clean
go clean
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [13:52:20]
→ ls
Makefile main.go
Improved source code
package main
import "fmt"
// Variables to identify the build
var (
Version = "1.0.0"
Build = "2017-02-14"
)
func main() {
fmt.Println("Version: ", Version)
fmt.Println("Build time: ", Build)
}
Improved Makefile
# This how we want to name the binary output
BINARY=gomake
# Builds the project
build:
go build -o ${BINARY}
# Installs our project: copies binaries
install:
go install
# Cleans our projects: deletes binaries
clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
.PHONY: clean install
Run
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:22:46]
→ ls -al
total 16
drwxr-xr-x 4 aliasliyu4 staff 136 Feb 14 14:22 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 292 Feb 14 14:22 Makefile
-rw-r--r-- 1 aliasliyu4 staff 203 Feb 14 14:03 main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:22:49]
→ make
go build -o gomake
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:22:51]
→ ls -al
total 3152
drwxr-xr-x 5 aliasliyu4 staff 170 Feb 14 14:22 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 292 Feb 14 14:22 Makefile
-rwxr-xr-x 1 aliasliyu4 staff 1603664 Feb 14 14:22 gomake
-rw-r--r-- 1 aliasliyu4 staff 203 Feb 14 14:03 main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:22:54]
→ ./gomake
Version: 1.0.0
Build time: 2017-02-14
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:22:58]
→ make clean
if [ -f gomake ] ; then rm gomake ; fi
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:23:04]
→ ls -al
total 16
drwxr-xr-x 4 aliasliyu4 staff 136 Feb 14 14:23 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 292 Feb 14 14:22 Makefile
-rw-r--r-- 1 aliasliyu4 staff 203 Feb 14 14:03 main.go
增加版本号
-a 编辑代码去突出版本号是痛苦的事情。
-b 你很快就会忘记它,提交,然后你的git历史也会断裂
添加构建时间
-a 和上述的版本号问题相似。
-b 使用哪种格式化的时间戳呢?怎么有这么奇怪的字符串?
-c 显然在编译之前也需要一个时钟去检查时间的准确性。
Improved source code
package main
import "fmt"
// Variables to identify the build
var (
Version string
Build string
)
func main() {
fmt.Println("Version: ", Version)
fmt.Println("Build time: ", Build)
}
Improved Makefile
# This how we want to name the binary output
BINARY=gomake
# These are the values we want to pass for VERSION and BUILD
VERSION=1.0.0
BUILD=`date +%FT%T%z`
# Setup the -Idflags options for go build here,interpolate the variable values
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}"
# Builds the project
build:
go build ${LDFLAGS} -o ${BINARY}
# Installs our project: copies binaries
install:
go install ${LDFLAGS}
# Cleans our projects: deletes binaries
clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
.PHONY: clean install
Run
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:59:14]
→ ls -al
total 3152
drwxr-xr-x 5 aliasliyu4 staff 170 Feb 14 14:59 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 562 Feb 14 14:59 Makefile
-rwxr-xr-x 1 aliasliyu4 staff 1603728 Feb 14 14:58 gomake
-rw-r--r-- 1 aliasliyu4 staff 192 Feb 14 14:53 main.go
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:59:17]
→ make
go build -ldflags "-X main.Version=1.0.0 -X main.Build=`date +%FT%T%z`" -o gomake
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:59:18]
→ ./gomake
Version: 1.0.0
Build time: 2017-02-14T14:59:18+0800
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:59:23]
→ make clean
if [ -f gomake ] ; then rm gomake ; fi
# aliasliyu4 at alialliyu4.local in ~/svn/src/gomake [14:59:28]
→ ls -al
total 16
drwxr-xr-x 4 aliasliyu4 staff 136 Feb 14 14:59 .
drwxr-xr-x 23 aliasliyu4 staff 782 Feb 14 13:22 ..
-rw-r--r-- 1 aliasliyu4 staff 562 Feb 14 14:59 Makefile
使用LDFLAGS
-a go链接命令允许你去设置字符串变量在编译的时候使用-X选项
-b 使用格式化:importpath.name=value
golang1.5+和之前版本的不同
-a golang1.5+ -X 选项作为一个分隔参数在“k=v”之间
-b 譬如: go build -ldflags "-X main.Version=1.0.0 -X main.Build=2017-02-14"
-c golang < 1.5 -X选项后面跟着两个分隔的参数
-d 譬如: go build -idflags "-X main.Version 1.0.0 -X main.Build 2017-02-14"