1、首先查看本地是否安装了go语言
brew info go

有此 Not installed 表示没有安装
2、安装go语言
(1)brew安装
brew install go
(2)下载安装包

3、配置go语言环境变量
go语言安装好后,默认的安装路径是:/usr/local/go,可以进去看看各种安装文件
打开 .bash_profile 文件,在最后输入
export GOPATH=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
source .bash_profile 文件,使之生效
4、查看go语言是否安装成功
查看安装的go版本:go version

5、报错解决
warning: GOPATH set to GOROOT (/usr/local/go) has no effect
表示,go语言的安装root目录是/usr/local/go,那么go语言的开发项目路径也是这个,就不太合适。因此,需要修改go语言项目的路径
示例:
export GOPATH=/Users/qkbj100574/work/go_project
修改后,再次使配置生效,然后再次查看版本就无报警了,查看go语言的环境变量配置信息也不再报警了
go env

6、初步开发,打印出hello world
在项目下建立 hello.go 文件,输入代码

运行go文件,
(1)直接运行文件:go run hello.go

(2)先编译,再运行编译文件
go build hello.go
会生成一个编译后的文件 hello

运行编译文件:./hello(注意:一定要加上./)

网友评论