macOS搭建go环境及VSCode安装使⽤教程
<语⾔环境搭建
在Mac上go语⾔环境搭建⽅式分为两种,⼀种是通过brew安装,⼀种是⼿动安装,这⾥笔者选择了后者,搭建步骤具体如下:
(1)下载安装包,
(2)将下载的安装包移⾄安装⽬录并解压
tar -zxvf go1.16.5. -C .
(3)配置环境变量
在.bash_profile⽂件中添加以下内容并使⽤source命令使之⽣效
//go项⽬的⼯作路径,该⽬录需要⾃建,并且需要在该⽬录下新建src、pkg以及bin⽂件夹
export GOPATH=/Users/sunxi/Documents/goCode
//go的安装路径
export GOROOT=/Users/sunxi/Documents/application/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
GOPATH下三个⽬录的含义如下:
src⽬录下主要存放go的源⽂件
pkg⽬录存放编译好的库⽂件,主要是*.a⽂件;
bin⽬录主要存放可执⾏⽂件
(4)验证安装结果
sunxi@sunxideMacBook-Pro ~ % go version
go version go1.16.5 darwin/amd64
2.安装VSCode并在其中安装go语⾔插件
安装VSCode直接在官⽅上下载即可,
go语⾔插件安装⼩技巧:可以在GOPATH⽬录下的src⽬录下新建⼀个⽂件,这时VSCode会提⽰安装安装插件,直接点击install all即可
插件安装结果:
Tools environment: GOPATH=/Users/sunxi/Documents/goCode, GOBIN=/Users/sunxi/Documents/application/go/bin
Installing 10 tools at the configured GOBIN: /Users/sunxi/Documents/application/go/bin in module mode.
gopkgs
go-outline
gotests
gomodifytags
impl
goplay
dlv
dlv-dap
staticcheck
gopls
Installing github/uudashr/gopkgs/v2/cmd/gopkgs (/Users/sunxi/Documents/application/go/bin/gopkgs) SUCCEEDED
Installing github/ramya-rao-a/go-outline (/Users/sunxi/Documents/application/go/bin/go-outline) SUCCEEDED
Installing github/cweill/gotests/gotests (/Users/sunxi/Documents/application/go/bin/gotests) SUCCEEDED
Installing github/fatih/gomodifytags (/Users/sunxi/Documents/application/go/bin/gomodifytags) SUCCEEDED
Installing github/josharian/impl (/Users/sunxi/Documents/application/go/bin/impl) SUCCEEDED
Installing github/haya14busa/goplay/cmd/goplay (/Users/sunxi/Documents/application/go/bin/goplay) SUCCEEDED
Installing github/go-delve/delve/cmd/dlv (/Users/sunxi/Documents/application/go/bin/dlv) SUCCEEDED
Installing github/go-delve/delve/cmd/dlv@master (/Users/sunxi/Documents/application/go/bin/dlv-dap) SUCCEEDED
/go/tools/cmd/staticcheck (/Users/sunxi/Documents/application/go/bin/staticcheck) SUCCEEDED
/x/tools/gopls (/Users/sunxi/Documents/application/go/bin/gopls) SUCCEEDED
All tools successfully installed. You are ready to Go :).
这⾥需要注意在安装插件时需要在.bash_profile中添加以下内容:
export GO111MODULE=on
export GOPROXY=goproxy.io
3.helloworld项⽬
在GOPATH/src下新建⼀个go-test项⽬,在项⽬下新建chapter_one⽬录并在该⽬录下新建⽂件,写个hello world程序package main
import"fmt"
func main(){
fmt.Println("hello,world!")
}
使⽤下⾯命令编译程序:
go
执⾏完该命令后会⽣成⼀个名为main的可执⾏的⼆进制⽂件,然后可以随时运⾏它
sunxi@sunxideMacBook-Pro chapter_one %./main
hello,world!
4.将本地的go-test项⽬上传⾄GitHub
(1)在GitHub上新建⼀个名为go-test的repository
(2)初始化本地仓库
git init
(3)添加当前⽬录下的所有⽂件到暂存区
git add .
(4)提交到本地仓库
go语言安装教程
git commit -m "hello,world"
(5)连接远程仓库
git remote add origin github/sunxi92/go-test.git
(6)将本地仓库提交到master分⽀上
git push -u origin master
后续继续更新代码⾄GitHub只需执⾏步骤(3)、(4)(6)即可

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。