Derick
337 words
2 minutes
Golang开发环境配置

推荐开发工具#

jetbrains家的goland (四星)

微软家的visual studio code (三星)

从golang官网下载最新SDK安装包

All releases - The Go Programming Language

通常情况下,会因为网络原因无法从golang module官网下载依赖,出现如下错误提示

go: finding module for package rsc.io/quote
go: LearnApp imports
        rsc.io/quote: module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 172.217.160.113:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

需要配置代理

Go GOROOT 与 GOPATH 介绍 GOROOT: GO 语言的安装路径。 GOPATH:若干工作区目录的路径 自己定义的工作空间。 GOBIN:GO 程序生成的可执行文件(executable file)的路径。

windows#

打开你的终端并执行:

go env -w GOPROXY=https://goproxy.cn,direct

macOS 或 Linux#

打开你的终端并执行:

$ export GOPROXY=https://goproxy.cn

或者

$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile && source ~/.profile

完成。

查看修改效果#

go env#

环境变量设置

go env -w xxx=xxx

go env -w GO111MODULE=on

这样就能正常下载golang的依赖包

或者配置运行时代理

创建proxy_go.go文件

package main

import (
	"net/http"
	"os"

	"github.com/goproxy/goproxy"
)

func main() {
	http.ListenAndServe("localhost:8080", &goproxy.Goproxy{
		GoBinEnv: append(
			os.Environ(),
			"GOPROXY=https://goproxy.cn,direct", // Use Goproxy.cn as the upstream proxy
			"GOPRIVATE=git.example.com",         // Solve the problem of pulling private modules
		),
		ProxiedSUMDBs: []string{
			"sum.golang.org https://goproxy.cn/sumdb/sum.golang.org", // Proxy the default checksum database
		},
	})
}

执行命令运行,配置goland代理为“http://localhost:8080”

go run proxy_go.go

更详细的说明参考:

Goproxy.cn

Golang开发环境配置
https://blog.ithuo.net/posts/golang-development-environment-configuration/
Author
Derick
Published at
2023-01-15