目录

Golang开发环境配置

现如今Go语言的开发阵容可以说是空前强大,且背靠Google这棵大树,又不乏牛人坐镇,是名副其实的“牛二代”。

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/93bb4268-aba8-4e9e-adb8-2eb516ab4d16.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

有关Go语言特性优缺点本文就不再赘述了,百度上一大堆。 

下载SDK

本文使用macOS操作系统为例,其他系统操作大同小异

下载地址:https://golang.org/dl/

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/34b9f2fa-5667-4940-b001-5e532d5bf020.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

安装SDK

双击下载后的pkg进行安装,安装成功后,打开终端,输入下面命令查看是否安装成功

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/bcb87be5-0483-40f3-9346-6bb378351469.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

环境变量配置

1
2
3
export GOPATH=/Users/wuwenze/Development/GoProjects
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

将以上内容写入到环境变量配置文件中(~/.bash_profile

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/8ddcbdf2-c225-4127-b1b3-b9b4563c1979.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

需要注意的是,如果你的macOS使用的是zsh终端,系统会加载~/.zshrc文件,并不会加载.bash_profile,所以需要在~/.zshrc中追加以下语句,使.bash_profile文件生效:

1
source ~/.bash_profile

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/da4b87ef-262e-4255-bb5b-49d65e87be06.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

环境变量说明

  • GOPATH:开发常用文件夹,可以配置多个。该文件夹下有3个文件夹(src/pkg/bin)

    • src:存放源代码文件
    • pkg:编译后生成的文件(.a文件)(非main函数的文件在go install后生成)
    • bin:存放编译后生成的可执行文件,可以自己执行
  • GOBIN:是GOPATH下的bin目录

Hello World

1
2
cd ~/Development/GoProjects/src
nano hello.go

编写Hello World源代码:

1
2
3
4
5
6
7
package main

import "fmt"

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

编译运行:

1
go run hello.go

https://wenzewoo-cdn.oss-cn-chengdu.aliyuncs.com/images/20190219/584d559b-a42d-4c38-9204-381d08116ba7.png?x-oss-process=image/auto-orient,1/interlace,1/quality,q_70/format,jpg

评论