GO 笔记

安装与配置(vscode)

  1. 安装go、安装vscode,安装完成后,打开命令行,输入go version,如果提示go的版本,说明安装成功
  2. 检查完善环境变量(正常情况下,安装go之后环境变量会自动设置)
    (1) 将go安装目录的bin目录添加到path中,以便执行go命令
    (2) 环境变量中添加了GOROOT,默认值为go的安装目录
    (3) 环境变量中添加了GOPATH,用来存放go下载的包和命令
  3. 配置代理,打开vscode,在settings.json中加入
{
    "http.proxySupport": "on",
    "http.proxy": "http://127.0.0.1:41091" // 修改为你的代理地址
}

4、安装插件,在vscode中点击左方的扩展按钮(Ctrl+Shift+X)
(1)搜索go,找到(Rich Go language support for Visual Studio Code),点击安装;
(2)搜索Code Runner,点击安装(安装完成后可通过右上角三角形按钮进行快速运行)。
(3)在vscode中新建一个go文件,vscode会自动提示安装其他所需的Go插件,点击全部安装即可

settings.json

{
    "http.proxySupport": "on",
    "http.proxy": "http://127.0.0.1:41091",
    "[go]":{
        "editor.codeActionsOnSave": {
            "source.organizeImports":false //保存时不自动删除import
        }
    }
}

初始化go模块

go env -w GOPROXY=https://goproxy.cn // 该命令为设置代理,防止被qiang,按需设置
// 还有其他代理如https://goproxy.io;http://mirrors.aliyun.com/goproxy/等)

go mod init example.com/hello // 在当前目录初始化模块管理,example.com/hello为项目名称,会生成 go.mod 文件
go mod  tidy // 根据 go.mod 文件来处理依赖关系

解决go模块管理代理网址无法访问:proxy.golang.org

 go mod tidy
go: finding module for package rsc.io/quote
example.com/main imports
        rsc.io/quote: module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 172.217.27.145: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.

解决方法:换一个国内能访问的代理地址:https://goproxy.cn,执行命令:

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

重新执行命令,完美通过!
还有其他代理如https://goproxy.io;http://mirrors.aliyun.com/goproxy/等

你可能感兴趣的:(GO 笔记)