mac m1的go环境配置及使用

mac m1的go环境配置及使用,第1张

1 go环境配置 1.1 下载go的安装包
# 下载地址
http://www.kaotop.com/file/tupian/20220518/pre                         image-20210716085430669.png
   
  
 1.2 配置环境变量 
 
# 分别在 .bash_profile 和 .zshrc 文件后面追加go的环境变量配置
# 说明:GOPATH表示go的工程根目录,之后的go程序均写在该目录下

# Go
export GOPATH=/Users/yorick/Documents/study/code/go-project
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
# Go END
# 使配置生效
source .bash_profile
source .zshrc

# 检验是否安装成功
go version
# 查看环境变量配置信息
go env
1.3 创建规范的目录结构

说明:

src目录中编写go的源程序,最好遵循如下的包命名规范,github.com表示github地址,yaokuku123为github用户名,在用户名目录中建立多个项目工程

image-20210716090024516.png 2 GoLand编辑器配置 2.1 下载GoLand编辑器(2020.3.4)

说明:最好下载2020.3.4版本的GoLand,网上说其他版本无法进行debug调试

image-20210716090343248.png 2.2 初始环境配置 GOROOT配置 image-20210716090438297.png GOPATH配置 image-20210716090508318.png 建立go的helloworld项目工程 image-20210716090608726.png 创建main方法,在该文件中编写helloworld程序,文件名可以任意 image-20210716090727475.png 编写代码
package main

import "fmt"

func main() {
    fmt.Println("hello world!")
}
点击运行按钮直接运行即可,打印出hello world 表示成功 image-20210716090901352.png 3 debugger配置

说明:Mac m1 的环境下,无法正常使用debug功能,需要我们做额外的配置使得可以正常使用debug功能

安装delve
# clone delve这个debug工具,并切换分支
git clone [email protected]:go-delve/delve.git # 防止使用http协议下载失败
cd delve
git checkout -b darwin-arm64-lldb

cd cmd/dlv
# 修改默认网站,防止timeout发生
go env -w GOPROXY=https://goproxy.cn
go build
go install  # 会将生成的二进制文件放到GOBIN定义的目录下
配置GoLand的配置 image-20210716091443730.png image-20210716091504064.png 编写测试程序
package main

import "fmt"

func main() {
    num1 := 10
    num2 := 20
    result := num1 + num2
    fmt.Println(result)
}
debug 即可,enjoy! image-20210716091654273.png

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995201.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)