博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设置Golang的开发环境
阅读量:2145 次
发布时间:2019-04-30

本文共 2350 字,大约阅读时间需要 7 分钟。

 

Golang开发环境涉及以下两个方面:

  • 语言环境
  • IDE设置

 

语言环境就是要安装golang,golang的安装有如下方式

Golang安装

1、安装文件安装

你可以在 这里下载golang的安装文件

2、包管理器安装

以ubuntu为例

sudo apt-get install golang

安装后可以查看下具体的安装版本

go version

由于我是mac我就直接brew install 安装

brew install go
  • 设置环境变量

golang的环境变量主要是下面三个

GOROOT:Go 语言安装根目录的路径,也就是 GO 语言的安装路径。

GOPATH:若干工作区目录的路径。是我们自己定义的工作空间。

GOPROXY:给go安装第三方包设置代理用的,由于github的访问问题一般设置国内镜像

参考极客时间 

GOROOT="/usr/local/Cellar/go/1.13.5/libexec"

GOPATH="/Users/workspace/go"

GOPROXY="https://goproxy.cn,direct"

添加到.zshrc文件中

echo "export GOPATH=~/workspace/go" >> ~/.zshrc

已知vscode有个报错和GOPATH和GOROOT设置有关,在import 包fmt报 "could not import fmt". 

就是GOPATH和GOROOT下没有go的src目录导致

 

  • IDE设置

我自己使用的VisualStudioCode(免费还好用)

1、安装go扩展

主要就一下几个

go官方go nightlygo doc

2、设置IDE go自动补全

参考  VS Code 中使用 gopls 补全 Go 代码

settings.json加上

"[go]": {        "editor.snippetSuggestions": "none",        "editor.insertSpaces": false,        "editor.formatOnSave": true,        "editor.codeActionsOnSave": {            "source.organizeImports": true        },        "editor.suggest.snippetsPreventQuickSuggestions": false    },    "gopls": {        "usePlaceholders": true // add parameter placeholders when completing a function    },

3、VSCode的go开发tools

通过cmd+shift+p ,command palette,执行Go:Install/Update Tools

全部勾选安装就行

 

4、设置Debug

暂时只设置了文件的调试,可以添加调试服务器debug

"configurations": [        {            "name": "Launch file",            "type": "go",            "request": "launch",            "mode": "debug",            "program": "${file}"        },    ]

 

写段简单代码验证下

package mainimport (	"fmt"	"sort")type pair struct {	key   string	value int}type pairSlice []pairfunc (p pairSlice) Len() int {	return len(p)}func (p pairSlice) Swap(i, j int) {	p[i], p[j] = p[j], p[i]}func (p pairSlice) Less(i, j int) bool {	return p[i].value < p[j].value}func main() {	var m = make(map[string]int)	m["admire"] = 0	m["blue"] = 1	m["acquire"] = 2	m["decend"] = 3	m["abuse"] = 4	var pairs = pairSlice{}	// 到这里map结构是很乱的,下面我们来排序	// map排序,无论怎么实现我理解都是再用一个容器来记录	// 有序的结构,然后迭代的时候就可以有序	for k, v := range m {		fmt.Println(k, ":", v)		p := new(pair)		p.key = k		p.value = v		pairs = append(pairs, *p)	}	fmt.Println(pairs)	// 打印分隔符	fmt.Println()	fmt.Println("-------------------------------")	sort.Stable(pairs)}

保存为sortmap.go,命令行执行下

$go run sortmap.goadmire : 0blue : 1acquire : 2decend : 3abuse : 4[{admire 0} {blue 1} {acquire 2} {decend 3} {abuse 4}]

 

 

转载地址:http://xzrgf.baihongyu.com/

你可能感兴趣的文章
强引用 软引用 弱引用 虚引用
查看>>
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
win10将IE11兼容ie10
查看>>
checkbox设置字体颜色
查看>>
第一篇 HelloWorld.java重新学起
查看>>
ORACLE表空间扩张
查看>>
orcal 循环执行sql
查看>>
web.xml配置监听器,加载数据库信息配置文件ServletContextListener
查看>>
结构型模式之桥接模式(Bridge)
查看>>
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>
C++/C 宏定义(define)中# ## 的含义 宏拼接
查看>>
Git安装配置
查看>>