Go开发环境搭建

Posted by 爱折腾的工程师 on Wednesday, January 3, 2018

背景

引用此处Go简介一段对Go描述的话: Go语言是由Google开发的一个开源项目,目的之一为了提高开发人员的编程效率。 Go语言语法灵活、简洁、清晰、高效。它对的并发特性可以方便地用于多核处理器 和网络开发,同时灵活新颖的类型系统可以方便地编写模块化的系统。go可以快速编译, 同时具有垃圾内存自动回收功能,并且还支持运行时反射。Go是一个高效、静态类型, 但是又具有解释语言的动态类型特征的系统级语法。

环境

  • MacOS Sierra (10.12.6)
  • Eclipse Oxygen Release (4.7.0)
  • GoClipse (0.16.1.v201607061938)
  • Go (1.9.2)
  • GDB (8.0.1)

此处针对MacOS + Eclipse搭建Go开发环境,这里不涉及MacOS、Eclipse安装配置

安装Go

从官网下载安装包:https://golang.org/dl/,默认安装目录是/usr/local/go

配置GOPATH、GOROOT环境变量,GOPATH路径与Eclipse workspaces路径保持一致

iceyaos-MacBook-Pro:~ iceyao$ vim ~/.bash_profile
# 添加如下内容
export GOPATH="/Users/iceyao/Documents"
export GOROOT="/usr/local/go"
export PATH="/usr/local/go/bin:$PATH"

立即让上述环境变量生效

iceyaos-MacBook-Pro:~ iceyao$ source ~/.bash_profile

验证go环境

iceyaos-MacBook-Pro:~ iceyao$ go
Go is a tool for managing Go source code.

Usage:

	go command [arguments]

The commands are:

	build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	env         print Go environment information
	bug         start a bug report
	fix         run go tool fix on packages
	fmt         run gofmt on package sources
	generate    generate Go files by processing source
	get         download and install packages and dependencies
	install     compile and install packages and dependencies
	list        list packages
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

	c           calling between Go and C
	buildmode   description of build modes
	filetype    file types
	gopath      GOPATH environment variable
	environment environment variables
	importpath  import path syntax
	packages    description of package lists
	testflag    description of testing flags
	testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

查看go环境变量

iceyaos-MacBook-Pro:~ iceyao$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/iceyao/Documents"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vm/ry3ff3355gnfvmgt8w17872c0000gn/T/go-build388809243=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

安装GoClipse

GoClipse是Eclipse的一个插件,顾名思义是为了运行Go程序

安装GoClipse插件,打开Eclipse,点击Help=>Install New Software

用科学上网方式安装会快点,安装完后重启生效

配置Go路径和安装Go tools

同样用科学上网方式安装gocode、guru、godef

测试Go程序

点击File -> New -> Go Project,创建一个test12的项目

在src目录下,创建tt文件夹,再在里面创建hello.go,内容如下

程序正常运行,说明Go环境是ok的

安装GDB

GDB用于调试Go程序,但在MacOS上想用GPB调试程序,还得用Keychain Access做证书

使用homebrew安装gdb

iceyaos-MacBook-Pro:~ iceyao$ brew update
iceyaos-MacBook-Pro:~ iceyao$ brew install gdb
iceyaos-MacBook-Pro:~ iceyao$ echo "set startup-with-shell off" >> ~/.gdbinit

打开Keychain Access,点击Certificate -> Create a Certificate

一路狂点,最后trust选择always trust,选择system, 创建完成后重启下电脑

证书签名

iceyaos-MacBook-Pro:~ iceyao$ codesign -s gdb-cert /usr/local/bin/gdb

Eclipse GDB配置,点击Eclipse -> Preferences

修改下hello.go,并双击设置个断点

能正常捕获到断点,说明GDB是ok的

参考链接

「真诚赞赏,手留余香」

爱折腾的工程师

真诚赞赏,手留余香

使用微信扫描二维码完成支付