1. Go编译器

两种官方编译器,gc和gccgo,其中gccgo基于gcc后端。

go编译器支持8种指令集,不同建构编译质量不同:

amd64 (also known )  
 (x86 or x86-)   Comparable to the amd64 port.
arm (ARM)
Supports Linux, FreeBSD, NetBSD, OpenBSD and Darwin binaries. Less widely used than the other ports.
arm64 (AArch64)
Supports Linux and Darwin binaries. New in 1.5 and not as well exercised as other ports.
ppc64, ppc64le (-bit PowerPC big- and little-endian)
Supports Linux binaries. New in 1.5 and not as well exercised as other ports.
mips, mipsle (-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.8 and not as well exercised as other ports.
mips64, mips64le (-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.6 and not as well exercised as other ports.
s390x (IBM System z)
Supports Linux binaries. New in 1.7 and not as well exercised as other ports.

go编译环境可以被定制,与平台和建构相关的是$GOOS和$GOARCH,分别指定目标操作系统和目标建构。常用组合如下:(注:$GOOS是darwin for macOS 10.1及以上和iOS)

$GOOS        $GOARCH
android     arm
darwin
darwin      amd64
darwin      arm
darwin      arm64
linux
linux       amd64
linux       arm
linux       arm64
windows
windows     amd64

go编译器(或go环境)安装

有两种安装方式:二进制发布包和源码包,参考https://golang.google.cn/doc/install。一般情况下可直接下载二进制发布包,官方已提供了常用平台的二进制发布包。

下载二进制tar包,tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz,一般安装路径为/usr/local,go工具命令要执行需要将/usr/local/go/bin导出到PATH环境变量中(/etc/profile可长期有效)。

源码包安装方式参考:Installing Go from source

ubuntu下可直接apt安装:

sudo apt-get install golang-go

go工具卸载

linux下直接删除/usr/local/go目录即可(同时修改PATH环境变量,或/etc/profile)。

GO环境变量

go env可打印go环境变量。

$GOPATH

GOPATH指定workspace位置,默认为$home/go,go项目在本地的开发环境的项目根路径(以便项目编译,go build, go install,go get)。若工作在其他目录,需设定GOPATH。注意GOPATH不能和go安装目录相同。

go env GOPATH

打印当前有效的GOPATH,若没有设置打印默认位置。

For convenience, add the workspace's bin subdirectory to your PATH:

$ export PATH=$PATH:$(go env GOPATH)/bin

GOPATH之下主要包含三个目录:bin,pkg,src。bin目录主要存放可执行文件;pkg目录存放编译好的库文件,主要是*.a文件;src目录下主要存放go的源文件。

$GOROOT

go的安装目录,配置后不会更改。一般为/usr/local/go或/usr/go或/usr/lib/go。

$GOROOT_FINAL

$GOOS and $GOARCH

用于不同平台的交叉编译,只需要在build之前设置这两个变量即可,这也是go语言的优势之一:可以编译生成跨平台运行的可执行文件。

注意:这个交叉编译暂不支持cgo方式,因此交叉编译时需要设置$CGO_ENABLED设置为0。

$GOHOSTOS and $GOHOSTARCH

$GOBIN

go二进制文件安装目录,默认为$GOROOT/bin。

$GO386

$GOARM

$GOMIPS

2. cgo

Cgo lets Go packages call C code.

The basics

If a Go source file imports "C", it is using cgo. The Go file will have access to anything appearing in the comment immediately preceding the line import "C", and will be linked against all other cgo comments in other Go files, and all C files included in the build process.

Note that there must be no blank lines in between the cgo comment and the import statement.

To access a symbol originating from the C side, use the package name C. That is, if you want to call the C function printf() from Go code, you write C.printf(). Since variable argument methods like printf aren't supported yet (issue 975), we will wrap it in the C method "myprint":

package main

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
    printf("%s", s);
}
*/
import "C"

import "unsafe"

func main() {
    cs := C.CString("Hello from stdio\n")
    C.myprint(cs)
    C.free(unsafe.Pointer(cs))
}

参考:

1.  http://golang.org/doc/articles/c_go_cgo.html

2. https://github.com/golang/go/wiki/cgo

3. http://wiki.jikexueyuan.com/project/go-command-tutorial/0.13.html  极客学院go命令详解

3. Go交叉编译

参考:Go cross compilation

If cgo is not required (common go programs, not including c/c++)

The go tool won’t require any bootstrapping if cgo is not required. That allows you to target the following program to any GOOS/GOARCH without requiring you to do any additional work. Invoke go build.

$ cat main.go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

In order to target android/arm, run the following command.

$ GOOS=android GOARCH=arm GOARM= go build .

The produced binary is targeting ARMv7 processors that runs Android. All possible GOOS and GOARCH values are listed on the environment docs.

If cgo is required (including c/c++)

If you need to have cgo enabled, the go tool allows you to provide custom C and C++ compilers via CC and CXX environment variables.

$ CGO_ENABLED= CC=android-armeabi-gcc CXX=android-armeabi-g++ \
    GOOS=android GOARCH=arm GOARM= go build .

The toolchain will invoke android-armeabi-gcc and android-armeabi-g++ if it is required to compile any part of the package with a C or C++ compiler. Consider the following program with a slightly different main function. Rather than outputting “hello world” to the standard I/O, it will use Android system libraries to write “hello world” to the system log.

$ cat main.go
// +build android

package main

// #cgo LDFLAGS: -llog
//
// #include <android/log.h>
//
// void hello() {
//   __android_log_print(
//     ANDROID_LOG_INFO, "MyProgram", "hello world");
// }
import "C"
func main() {
    C.hello()
}

If you build the program with the command above and examine the build with -x, you can observe that cgo is delegating the C compilation to arm-linux-androideabi-gcc.

$ CGO_ENABLED= \
CC=arm-linux-androideabi-gcc \
CXX=arm-linux-androideabi-g++ \
GOOS=android GOARCH=arm GOARM= go build -x .
...
CGO_LDFLAGS=”-g” “-O2” “-llog” /Users/jbd/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/rakyll/hello/_obj/ -importpath github.com/rakyll/hello — -I $WORK/github.com/rakyll/hello/_obj/ main.go
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length= -print-libgcc-file-name
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length= -I $WORK/github.com/rakyll/hello/_obj/ -g -O2 -o $WORK/github.com/rakyll/hello/_obj/_cgo_main.o -c $WORK/github.com/rakyll/hello/_obj/_cgo_main.c
...

Pre-building the standard library

The go tool also provides a utility if you would like to pre-build the standard library, targeting a specific GOOS and GOARCH.

$ CGO_ENABLED= \
    CC=arm-linux-androideabi-gcc \
    CXX=arm-linux-androideabi-g++ \
    GOOS=android GOARCH=arm GOARM= go install std

The standard library targeting android/armv7 will be available at $GOROOT/pkg/android_arm.

$ ls $GOROOT/pkg/android_arm
archive    fmt.a      math       runtime.a
bufio.a    go         math.a     sort.a
bytes.a    hash       mime       strconv.a
compress   hash.a     mime.a     strings.a
container  html       net        sync
crypto     html.a     net.a      sync.a
crypto.a   image      os         syscall.a
database   image.a    os.a       testing
debug      index      path       testing.a
encoding   internal   path.a     text
encoding.a io         reflect.a  time.a
errors.a   io.a       regexp     unicode
expvar.a   log        regexp.a   unicode.a
flag.a     log.a      runtime

If you prefer not to pre-build and install the standard library to the GOROOT, required libraries will be built while building user packages. But, the standard libraries builds are not preserved for future use at this stage and they will be rebuilt each time you run go build.

最新文章

  1. linux的常用文件系统格式
  2. javascript作用域中令你意想不到的问题
  3. C++标准库 -- tuple
  4. JavaScript标准库之 - Math
  5. 0728关于html的几个基础知识点
  6. ajax 之js读取xml的多浏览器兼容
  7. 【20160924】GOCVHelper MFC增强算法(1)
  8. 原生js显示分页效果
  9. Javascript学习总结三(Array对象的用法)
  10. WinDbug抓取进程dump
  11. 捷易拍与springMVC系统结合
  12. Activiti+oracle 启动项目时不能自动建表或更新表的问题分析及解决办法
  13. python初学笔记(二)
  14. Lightoj 1066 Gathering Food (bfs)
  15. 初学者易上手的SSH-struts2 05拦截器与自定义拦截器
  16. transition-timing-function 属性
  17. CentOS 7 Squid代理服务器正向代理-传统代理
  18. springmvc的Controller里实现转发的同时弹出提示对话框
  19. Git科普来一发:【rebase】与【merge】
  20. Word Ladder II leetcode java

热门文章

  1. php file_get_contents读取大容量文件方法
  2. javascript some()函数用法详解
  3. Ubuntu 添加安装字体
  4. 神经网络写诗(charRNN)
  5. 使用Spring4时, 运行时出现找不到MappingJacksonHttpMessageConverter的情况
  6. andorid 直接解压后的xml的解密
  7. transfer learning
  8. 数据库的ACID
  9. VS注释与取消注释快捷键
  10. [转]Unity3D新手引导开发手记