Golang
约 5 分钟阅读
实践
- 尽量避免 cgo
- 重复代码尽量 生成
go get不会下载源码到~/go/src了 - #31529GitHub Issuegolang/go#31529- c-shared 不支持 musl - golang/go#13492GitHub Issuegolang/go#13492尽量避免 cgo · 重复代码尽量 生成 · 不会下载源码到 了 - #31529 · c-shared 不支持 musl - golang/go#13492 · json omitempty 不支持 struct golang/go#11939笔记:Golang
- json omitempty 不支持 struct golang/go#11939GitHub Issuegolang/go#11939尽量避免 cgo · 重复代码尽量 生成 · 不会下载源码到 了 - #31529 · c-shared 不支持 musl - golang/go#13492 · json omitempty 不支持 struct golang/go#11939笔记:Golang
-
参考
-
Websocket C10M
Directory and file names that begin with "." or _ are ignored by the go tool, as are directories named "testdata".
# 更新 Go 过后可能会导致每个项目都会重复构建, 导致编译很慢, 针对单个项目可以按照如下的方式处理
# 把所有的依赖从新编译
go build -v 2> /tmp/build-tmp
sed -i '$ d' /tmp/build-tmp
# 更新所有依赖
cat /tmp/build-tmp | xargs -n 1 go get -u -v
# go build -v 2> /tmp/build-tmp;sed -i '$ d' /tmp/build-tmp;cat /tmp/build-tmp | xargs -n 1 go get -u -v
# 重新构建并缓存, 这样下次构建就会很快了
go build -i -v ./...
# 跨平台编译
env GOOS=linux GOARCH=amd64 go build -o main-linux-amd64 main.go
# 移动包
go get github.com/golang/tools/cmd/gomvpkg/main.go
gomvpkg -from github.com/wenerme/before -to github.com/wenerme/after
# 将构建时间添加到生成的内容中
go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go
go run -ldflags "-X main.xyz=abc" main.go
go run -ldflags "-X main.build=`date +%Y%m%d.%H%M%S`" main.go
# Guru
go get github.com/golang/tools/cmd/guru/main.go依赖包github.com/golang/tools/cmd/gomvpkg/main.goGo 包github.com/golang/tools/cmd/gomvpkg/main.goGogithub.com/golang/tools/cmd/guru/main.goGo 包github.com/golang/tools/cmd/guru/main.goGo
Install golang under linux
GOVERSION=1.7.3
# 查看可选架构 https://storage.googleapis.com/golang/
# Windows 64 位 https://storage.googleapis.com/golang/go$GOVERSION.windows-amd64.zip
# Windows 32 位 https://storage.googleapis.com/golang/go$GOVERSION.windows-386.zip
wget https://storage.googleapis.com/golang/go$GOVERSION.linux-amd64.tar.gz
# 或者使用代理下载
# https_proxy=socks://127.0.0.1:8888 curl https://storage.googleapis.com/golang/go$GOVERSION.linux-amd64.tar.gz -o go$GOVERSION.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go$GOVERSION.linux-amd64.tar.gz
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
# 或将环境变量放到启动脚本
cd ~
echo 'export GOROOT=/usr/local/go' >> $HOME/.bashrc
echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $HOME/.bashrc
source $HOME/.bashrcUNINSTALL
sudo rm -rf /usr/local/go与 C 交互操作/Interop with C
go install -buildmode=shared -linkshared std
go install -buildmode=shared -linkshared userownpackage
go build -linkshared yourprogramExport function
//export SayHello
func SayHello(name string) {
fmt.Printf("Nautilus says: Hello, %s!\n", name)
}Inline C code
// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
// return f();
// }
//
// int fortytwo()
// {
// return 42;
// }
import "C"
import "fmt"
func main() {
f := C.intFunc(C.fortytwo)
fmt.Println(int(C.bridge_int_func(f)))
// Output: 42
}Reference
交叉编译/Cross compile
# 编译为 Linux 可执行文件
env GOOS=linux GOARCH=amd64 go build -o RedHat/clbeat -v github.com/wenerme/clbeat
# 编译为 Windows 可执行文件
env GOOS=windows GOARCH=amd64 go build -o main.exe -v部分需要 linux cgo 编译的可使用 docker 镜像完成
docker run --rm -v $GOPATH:/go -w /go/src/应用包 golang go build -i -v
# 因为是使用 alphie 编译的,因此构建的 docker 中需要添加
# RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
# 需要注意 https://github.com/golang/go/issues/9344xgo
可使用 xgo 一次性编译多个平台的可执行文件,可使用 xgo 镜像以便于跨平台编译 cgo.
- 查看所有支持的环境 environment
Go & C++
使用 Go 和 C++ 可通过 Swig 实现,也可通过将 C++ 的方法全封装为 C 方法,然后再通过 Go 调用. 在量较少的时候,使用第二种方式是非常方便快捷的,但是如果想要把大量的接口导出到 Go, 并且保持类特性,则只能使用 Swig.
Go 自 1.1 开就支持 Swig 了.
- Swig Go ExampleGitHub 文件swig/swig/Examples/go尽量避免 cgo · 重复代码尽量 生成 · 不会下载源码到 了 - #31529 · c-shared 不支持 musl - golang/go#13492 · json omitempty 不支持 struct golang/go#11939笔记:Golang
# 相关帮助
swig -go -help
swig -go -intgosize 64 -c++ -cgo director.i
go installswig -go --help
Go Options (available with -go)
-cgo - Generate cgo input files
-gccgo - Generate code for gccgo rather than 6g/8g
-go-pkgpath <p> - Like gccgo -fgo-pkgpath option
-go-prefix <p> - Like gccgo -fgo-prefix option
-intgosize <s> - Set size of Go int type--32 or 64 bits
-package <name> - Set name of the Go package to <name>
-use-shlib - Force use of a shared library
-soname <name> - Set shared library holding C/C++ code to <name>程序瘦身/Reduce binary size
# A Hello world in Golang 1.6 is 2.2M
# 2.1M
strip main
# -s disable symbol table
# -w disable DWARF generation
# 1.7M
go build -ldflags "-s -w" main.go
# UPX can not compress drawin.amd64
env GOOS=linux GOARCH=amd64 go build -o main.linux.amd64 main.go # 2.2M
env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o main.linux.amd64.flag main.go # 1.6M
upx --best main.linux.amd64 # 666K
upx -9 --ultra-brute main.linux.amd64 # 508K
upx --best main.linux.amd64 # 478K
upx -9 --ultra-brute main.linux.amd64 # 363KProfiling
blog.golang.org/profiling-go-programs golang.org/pkg/net/http/pprof
Beego
go get -u github.com/beego/bee
go get -u github.com/astaxie/beego
bee api bapi
cd bapi
bee run -downdoc=true -gendoc=true依赖包github.com/beego/beeGo 包github.com/beego/beeGogithub.com/astaxie/beegoGo 包github.com/astaxie/beegoGo
Self Update
参考/Reference
- Articles
- Video
- cmd/go链接golang.org/cmd/go尽量避免 cgo · 重复代码尽量 生成 · 不会下载源码到 了 - #31529 · c-shared 不支持 musl - golang/go#13492 · json omitempty 不支持 struct golang/go#11939笔记:Golang
- vender
- Summary of Go Generics Discussions
- FAQ
- The Three Go Landmines
- pkg/plugin
- Calling Go Functions from Other Languages
学习资源
- 《Go 编程基础》GitHubUnknwon/go-fundamental-programming 是一套针对 Google 出品的 Go 语言的视频语音教程,主要面向新手级别的学习者。
- Learning-GoGitHubmikespook/Learning-Go-zh-cn
- Go Web 基础GitHubUnknwon/go-web-foundation
- Go 名库讲解GitHubUnknwon/go-rock-libraries-showcases
- Go 语言学习资料与社区索引GitHubUnknwon/go-study-index
FAQ
什么时候使用指针
- 方法接收使用指针
- 当有疑问时, 使用指针
- 当结构体比较大, 或会发生变化时使用指针
为枚举生成 String 方法
- 使用 Stringer 生成
- 生成的默认文件为
<type>_string.go
// 为类型 MyType 生成 Stringer
//go:generate stringer -type=MyType
// 输出到 strings.go
//go:generate stringer -type=MyType -output=strings.go关联信息
反向链接、本文链接的其他页面和外部资料。
参考资料
GitHub10 条
- golang/go#11939github.com/golang/go/issues/11939
- golang/go#13492github.com/golang/go/issues/13492
- golang/go#31529github.com/golang/go/issues/31529
- jpillora/overseergithub.com/jpillora/overseer
- mikespook/Learning-Go-zh-cngithub.com/mikespook/Learning-Go-zh-cn
- swig/swig/Examples/gogithub.com/swig/swig/tree/master/Examples/go
- Unknwon/go-fundamental-programminggithub.com/Unknwon/go-fundamental-programming
- Unknwon/go-rock-libraries-showcasesgithub.com/Unknwon/go-rock-libraries-showcases
- Unknwon/go-study-indexgithub.com/Unknwon/go-study-index
- Unknwon/go-web-foundationgithub.com/Unknwon/go-web-foundation
其他外链21 条
- blog.golang.org/profiling-go-programsblog.golang.org/profiling-go-programs
- blog.ralch.com/tutorial/golang-sharing-librariesblog.ralch.com/tutorial/golang-sharing-libraries
- dev.to/vladimirvivien/calling-go-functions-from-other-languagesdev.to/vladimirvivien/calling-go-functions-from-other-languages
- docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/editdocs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/edit
- docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/editdocs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/edit
- docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4/editdocs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4/edit
- gist.github.com/lavalamp/4bd23295a9f32706a48fgist.github.com/lavalamp/4bd23295a9f32706a48f
- godoc.org/golang.org/x/tools/cmd/stringergodoc.org/golang.org/x/tools/cmd/stringer
- golang.org/cmd/cgogolang.org/cmd/cgo
- golang.org/cmd/gogolang.org/cmd/go
- golang.org/doc/faqgolang.org/doc/faq
- golang.org/doc/install/sourcegolang.org/doc/install/source
- golang.org/pkg/net/http/pprofgolang.org/pkg/net/http/pprof
- goroutines.com/10mgoroutines.com/10m
- mholt.github.io/curl-to-gomholt.github.io/curl-to-go
- mholt.github.io/json-to-gomholt.github.io/json-to-go
- tip.golang.org/pkg/plugintip.golang.org/pkg/plugin
- www.ardanlabs.com/blog/2017/06/design-philosophy-on-data-and-semantics.htmlwww.ardanlabs.com/blog/2017/06/design-philosophy-on-data-and-semantics.html
- www.infoq.com/news/2016/03/go-patternswww.infoq.com/news/2016/03/go-patterns
- www.swig.org/Doc3.0/SWIGDocumentation.htmlwww.swig.org/Doc3.0/SWIGDocumentation.html
- www.youtube.com/watch?v=xxDZuPEgbBUwww.youtube.com/watch?v=xxDZuPEgbBU