golang环境搭建
安装golang
可以去golang中国网站下载安装:https://golang.google.cn/
选择对应操作系统的golang,下载安装即可
设置环境变量
查看本机shell
echo $SHELL
macbook高版本系统默认使用zsh
如果是bash,在bash_profile中设置环境变量
vim ~/.bash_profile
如果是zsh,在zshrc中设置环境变量
vim ~/.zshrc
然后按E键进入编辑模式
添加以下配置
# gopath
export GOPATH=/Users/admin/Documents/gopath
# gobin
export GOBIN=$GOPATH/bin
# 添加到环境变量
export PATH=$PATH:$GOBIN
# 代理,先去国内的代理,没有就直连
GOPROXY="https://goproxy.cn,direct"
# 校验和数据库服务地址
export GOSUMDB="sum.golang.google.cn"
# 开启go mod
export GO111MODULE=on
按ESC退出编辑模式,输入:wq,保存退出
然后source一下对应的配置文件
source ~/.bash_profile
source ~/.zshrc
然后可以输入一下go env查看golang的开发环境
开发准备
去上面设置的GOPATH目录下,新建src文件夹,后面所有的golang源代码都存放到这个目录中
项目路径一般为:$GOPATH/src/代码托管的网址/(作者|机构)/项目名
例如:$GOPATH/src/github.com/go-sql-driver/mysql
新建项目:$GOPATH/src/gitee.com/anote/demo
使用goland作为ide,新建main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("hello golang")
}
点击运行
goland一些设置
设置gopath
- 第一个勾选了,表示使用系统环境变量中设置的gopath路径
- 第二个勾选了,会将当前项目作为gopath
设置gomodule
勾选enable go module integration,使用go module
还没有评论,来说两句吧...