0. 简述

Go是一个开源的编程语言,它能让构造简单、可靠且高效的软件变得容易。

Go语言被设计成一门应用于搭建web服务器,存储集群或类似用途的巨型中央服务器的系统编程语言。对于高性能分布式系统领域而言,Go语言无疑比大多数其他语言有着更高的开发效率。它提供了海量并行的支持,这对于游戏服务器端的开发而言是再好不过的。

Go官网:https://golang.google.cn/或https://github.com/golang/go

package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

X86上编译运行:

wang@ubuntu:~/go$ go build -o hello hello.go
wang@ubuntu:~/go$ ./hello
Hello, World!

或直接运行

wang@ubuntu:~/go$ go run hello.go
Hello, World!

X86平台交叉编译ARM64平台上程序:

GOOS=linux GOARCH=arm64 go build -o hello hello.go
wang@ubuntu:~/go$ file hello
hello: ELF -bit LSB executable, ARM aarch64, version (SYSV), statically linked, not stripped

godoc用法:

 godoc -http=localhost: -play

godoc可提供离线的go文档,便于本地查看。

可在https://godoc.org/中搜索所有golang库的接口说明。

1. Go编译器

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

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

amd64 (also known as x86-64)  
386 (x86 or x86-32) 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 (64-bit PowerPC big- and little-endian)
Supports Linux binaries. New in 1.5 and not as well exercised as other ports.
mips, mipsle (32-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.8 and not as well exercised as other ports.
mips64, mips64le (64-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 386
darwin amd64
darwin arm
darwin arm64
linux 386
linux amd64
linux arm
linux arm64
windows 386
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。export GOPATH=$HOME/go

或直接写到/etc/profile中,然后source /etc/profile

注意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

集成开发环境IDE

vscode-golang配置参考VS code golang 开发环境搭建

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命令详解

4.  https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/readme.html Go语言高级编程(Advanced Go Programming)cgo编程

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=7 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=1 CC=android-armeabi-gcc CXX=android-armeabi-g++ \
GOOS=android GOARCH=arm GOARM=7 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=1 \
CC=arm-linux-androideabi-gcc \
CXX=arm-linux-androideabi-g++ \
GOOS=android GOARCH=arm GOARM=7 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=0 -print-libgcc-file-name
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -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=1 \
CC=arm-linux-androideabi-gcc \
CXX=arm-linux-androideabi-g++ \
GOOS=android GOARCH=arm GOARM=7 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.

go语言学习参考:

1. Go 系列教程(Golang tutorial series) go语言中文网

2. go入门指南 博客

3. go语言学习 博客

4. go基础学习 coder python修行路

5.  beego项目https://beego.me

6. https://github.com/golang/go

7. How to write Go Code https://golang.google.cn/doc/code.html

8. https://golang.google.cn/ 提供go在线测试环境和文档

9. https://golang.google.cn/doc/ go相关文档

10. https://golang.google.cn/pkg/ go标准库

11.  https://godoc.org/ go实用库搜索

12. https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/readme.html Go语言高级编程(Advanced Go Programming)

13. 在 GitHub 上构建一个看上去正规的 Golang 项目

14. Go 语言设计与实现

go语言简述的更多相关文章

  1. Dart 语言简述

    Dart是一种“结构化的web编程”语言,Dart编程语言在所有现代浏览器和环境中提供高性能.Dart是谷歌开发的计算机编程语言,后来被ECMA认定为标准. Dart重要的概念: 1.所有的东西都是对 ...

  2. iOS开发基础--C语言简述(一)

    先占个坑,回来再补 需要的运行环境,自行搜寻,工具不止一种,不详细叙述. C语言是一门非常重要的编程语言,与硬件底层直接相关,很多语言到最后的接口封装都会选择C语言,因而C语言一直很受欢迎,也务必掌握 ...

  3. (一)羽夏看C语言——简述

    "羽夏看C语言"介绍什么   本系列从汇编的角度,比较翔实的介绍C语言.C++和C其实是一样的东西,C++的编译器只是更强大,更能帮助我们写代码,例如模板.没有特殊说明,本系列不会 ...

  4. Python语言——Python语言概述

    Python语言概述 计算机语言概述 语言:交流工具,沟通媒介 计算机语言:人和计算机交流的工具,翻译官 Python语言简述 Python是计算机语言的一种 Python编程语言: 代码:人类语言, ...

  5. C语言学习书籍推荐《学通C语言的24堂课》下载

    下载地址:点我 编辑推荐 <学通C语言的24堂课>:用持续激励培养良好习惯以良好习惯铸就伟大梦想——致亲爱的读者朋友在开始学习<学通C语言的24堂课>的同时,强烈建议读者朋友同 ...

  6. Procomm Plus 与ASPECT脚本语言在基于远程终端设备上的测试应用

    产测 ---------------------------------------------------- 原文:http://www.bixuanzl.com/20180801/1084478. ...

  7. Go语言开发教程

    Go语言简述 兴起:2009年Gogle发布的第二款开源编程语言 特征: 语法简单:语法标准比较严格,适合开发人员短时间高效的服务端开发. 并发模型:Goroutine是Go最显著的特征,使用类协程的 ...

  8. (六)羽夏看C语言——函数

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

  9. (七)羽夏看C语言——模板(C++)

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

随机推荐

  1. Python 文件 flush() 方法

    描述 Python 文件 flush() 方法是用来把文件从内存buffer(缓冲区)中强制刷新到硬盘中,同时清空缓冲区. 一般情况下,文件关闭后会自动刷新到硬盘中,但有时你需要在关闭前刷新到硬盘中, ...

  2. BIO NIO AIO 简介

    原文: https://github.com/zhongmingmao/nio_demo 简介 NIO与AIO的简单使用 基本概念 同步与异步 同步和异步是针对应用程序和内核的交互而言的:同步指的是用 ...

  3. FIR仿真module_04

    作者:桂. 时间:2018-02-06  12:10:14 链接:http://www.cnblogs.com/xingshansi/p/8421001.html 前言 本文主要记录基本的FIR实现, ...

  4. thinkphp AOP(面向切面编程)钩子和行为

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...

  5. 【Android API】Android 4.1 API官方文档详解

    原文:http://android.eoe.cn/topic/summary 翻译:[eoeAndroid原创团队]kris.流风而逝.贼寇在何方.snowxwyo.lsy4833406 更新日期:2 ...

  6. 关于Eclipse如何加入Gradle文件与Android Studio两个平台一起开发,工作目录不发生变化

    前言: 本来很久之前想弄这玩意,不过因为各种原因,所以没弄. 今天有位前辈提出需求.说想让Eclipse的Android项目逐步走向Android Studio,但是又担心Android Studio ...

  7. [na]TCP的三次握手四次挥手/SYN泛洪

    1.TCP报文格式 上图中有几个字段需要重点介绍下: (1)序号:Seq序号,占32位,用来标识从TCP源端向目的端发送的字节流,发起方发送数据时对此进行标记. (2)确认序号:Ack序号,占32位, ...

  8. 【Unity】9.3 粒子系统生成器详解

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 上一节已经介绍过了在Unity 5.x中两种创建粒子效果的方式(方式1.方式2). 这一节我们主要学习第2种方式的基本概 ...

  9. hibernate的flush()、refresh()、clear()针对一级缓存的操作的区别

    首先session是有一级缓存的,目的是为了减少查询数据库的时间,提高效率,一级缓存的生命周期和session是一样的, session.flush()和session.clear()就针对sessi ...

  10. 4-4-串的KMP匹配算法-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第4章  串 - KMP匹配算法 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码 ...