Go's mac environment construction, and simple compilation.

http://blog.csdn.net/listron/article/details/54861603

Try Go language.

Go language is the main language promoted by Google. As a new static type development language, it has many exciting new features compared with the current development language. Specially optimized for application programming of multi-processor systems, the use of go language is completely comparable to the speed of c and c++, and it is safer, more concise, and supports parallel processes.

The following are the main features of the go language:

1. Automatic garbage collection

2. Richer built-in types

3. Functions return multiple values

4. Error handling

5. Anonymous functions and closures

6. Types and Interfaces

7. Concurrent programming

8. Reflection

9. Language Interactivity

Mac development environment to build

The above is basically nonsense. It is the kingly way to build a development environment and start the programming journey as soon as possible. The author uses a mac system, so I will introduce the environment construction of golang under mac.

1. Install the Golang SDK

The download address of Google's official website is as follows:  http://www.golangtc.com/download  , download the latest installation package, and then double-click to install it.

After the installation is complete, open the terminal, enter go, or go version (to view the installation version) and the following information appears, indicating that the installation is successful:

 
 

zhangqiangdeMacBook-Pro:gg zhangqiang$ go version

go version go1.8beta1 darwin/amd64

2. Configure environment variables

After installing the sdk, the next step is to configure the environment variables. Open the terminal and enter cd ~ to enter the user's home directory, and then enter the ls -all command to check whether .bash_profile exists.

file, both open and edit the file using vim .bash_profile. According to your actual situation, the content is as follows:

export GOPATH=/Users/zhangqiang/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

GOPATH:日常开发的根目录。GOBIN:是GOPATH下的bin目录。

且需要gobin目录加入到path路径下,生成的可执行文件就可以直接运行了。

退出vim,使用source ~/.bash_profile即可完成对golang环境变量的配置了,可以在终端中输入go env查看配置后的效果:

zhangqiangdeMacBook-Pro:~ zhangqiang$ go env

GOARCH="amd64"

GOBIN="/Users/zhangqiang/go/bin"

GOEXE=""

GOHOSTARCH="amd64"

GOHOSTOS="darwin"

GOOS="darwin"

GOPATH="/Users/zhangqiang/go"

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/p2/z4cgb3kd3mldyl7fxm6jjv3w0000gn/T/go-build485816814=/tmp/go-build -gno-record-gcc-switches -fno-common"

CXX="clang++"

CGO_ENABLED="1"

PKG_CONFIG="pkg-config"

CGO_CFLAGS="-g -O2"

CGO_CPPFLAGS=""

CGO_CXXFLAGS="-g -O2"

CGO_FFLAGS="-g -O2"

CGO_LDFLAGS="-g -O2"

3、开发工具配置(sublime text)

这里笔者选用的是sublime text安装gosublime插件进行开发(golang语法高亮提示),安装方式这里就不多提了。


goProject是go的workspace。

配置好开发环境之后,简单的进行一下go语言的开发。在你的gopath下的src目录下,新建一个文件夹(以项目名称命名,这里是gg),然后在sublime中打开该文件夹,新建main.go文件即可以进行编码了。

package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello zhangqiang!");
}

代码编写完成之后,使用command+b打开sublime text终端,使用go build gg(项目名称)对其进行编译,编译通过的结果信息如下

[ `go build hello_world` | done: 599.030542ms ]
   [ ~/goProject/src/hello_world/ ] # 

提示编译成功之后,再执行shell命令,执行刚刚编译之后的文件./gg即可看到运行结果:

[ `./hello_world` | done: 23.823761ms ]
	hello world!

如果仅仅是只需要看到运行的结果,而不产生可执行文件(文件名和项目名一样)则在sublime text终端中直接使用go run xxx.go即可:

[ `go run gg.go` | done: 553.566407ms ]
	hello world!

到目前为止,我们已经安装好了开发golang程序的基本环境,可以开心的享受golang的奇妙之处了!

注意:

1:在GOPATH的src路径中,建在执行中,经常遇到

can't load package: package gg: no buildable Go source files in /Users/zhangqiang/go/src/gg  错误,

经过2个小时的排查,发现,创建的main.go文件,并不是main.go,而是main.go.txt文件,原因是我用的是txt文本编辑器,默认是txt的后缀名,只是给隐藏了。需要下载类似sublime text的软件,编辑go代码。

2: The gosublime plugin needs to be installed. At present, it needs to upgrade sublime text to 3. Download address: https://www.sublimetext.com/3


3: Execute go in gosublime's terminal instead of mac's own terminal. command+b to open the sublime text terminal


references:

http://www.linuxidc.com/Linux/2015-05/117283.htm

http://www.tuicool.com/articles/Fv6zUfE


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324959369&siteId=291194637