Go語言環境安裝篇


回家想重新學習下go的源碼,然后在win上安裝問題多多,遂整理一下

一、Go安裝包下載地址:

國情原因 https://golang.org/ 偶爾會訪問不了,新手要下載 Go 官方的提供的安裝包經常會受阻。解決方法是使用可信任的其他鏡像進行下載,推薦這兩個:

https://golang.google.cn/dl/
https://gomirrors.org/

 

 

 

 二、路徑配置:

win下:

    下載后直接雙擊msi文件安裝,默認安裝在C:\Program Files\Go
    安裝完成后默認會在環境變量 Path 后添加 Go 安裝目錄下的 bin 目錄 C:\Program Files\Go\bin\,並添加環境變量 GOROOT,值為 Go 安裝根目錄 C:\Program Files\Go\
    驗證是否安裝成功,在運行中輸入 cmd 打開命令行工具,在提示符下輸入 go

    

 

添加國內代理,否則很多包無法下載:

   將Go默認的代理替換為以下代理:

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

 三、IDE的選擇和設置:

目前可選的IDE有很多,如VsCode,goland,subline等,強烈推薦用Vscode,然后使用glide做包管理。

vs插件安裝

      

 

 

   vscode-go 配置

    settins.json 基本上不需要配置,用默認值就可以了。如需調整 文件 -->首選項 -->設置 輸入go即可查看go相關配置

{
    "git.ignoreLimitWarning": true,
    //開啟自動保存
    "files.autoSave": "onFocusChange",
    "go.buildFlags": [],
    "go.lintFlags": [],
    "go.useCodeSnippetsOnFunctionSuggest": false,
    "[go]": {
        "editor.insertSpaces": false,
        "editor.formatOnSave": true
    },
    "go.formatTool": "goreturns",
    "go.goroot": "C:\\Go",
    "go.gopath": "D:\\GoPath"
}

  

安裝GO相關組件:

      官方文檔說明 https://github.com/Microsoft/vscode-go/wiki/Go-tools-that-the-Go-extension-depends-on
      vscode也可以通過ctrl+shift+p 運行命令一次性安裝所有這些工具

       Go: Install/Update Tools

然后呢,在下載go的相關包的過程中遇到問題:

  由於各種原因下載不下來,於是重新手動下載下載

解決方案:

          

// 進入該目錄下的src目錄
cd $GOPATH/src

//創建github.com插件目錄及下載插件
cd $GOPATH/src
mkdir github.com
cd $GOPATH/src/github.com
mkdir acroca cweill derekparker go-delve josharian karrick mdempsky pkg ramya-rao-a rogpeppe sqs uudashr
cd $GOPATH/src/github.com/acroca
git clone https://github.com/acroca/go-symbols.git
cd $GOPATH/src/github.com/cweill
git clone https://github.com/cweill/gotests.git
cd $GOPATH/src/github.com/derekparker
git clone https://github.com/derekparker/delve.git
cd $GOPATH/src/github.com/go-delve
git clone https://github.com/go-delve\delve.git
cd $GOPATH/src/github.com/josharian
git clone https://github.com/josharian/impl.git
cd $GOPATH/src/github.com/karrick
git clone https://github.com/karrick/godirwalk.git
cd $GOPATH/src/github.com/mdempsky
git clone https://github.com/mdempsky/gocode.git
cd $GOPATH/src/github.com/pkg
git clone https://github.com/pkg/errors.git
cd $GOPATH/src/github.com/ramya-rao-a
git clone https://github.com/ramya-rao-a/go-outline.git
cd $GOPATH/src/github.com/rogpeppe
git clone https://github.com/rogpeppe/godef.git
cd $GOPATH/src/github.com/sqs
git clone https://github.com/sqs/goreturns.git
cd $GOPATH/src/github.com/uudashr
git clone https://github.com/uudashr/gopkgs.git

// 創建golang.org插件目錄及下載插件
cd $GOPATH/src
mkdir -p golang.org/x
cd golang.org/x
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint.git

// 手動安裝插件
cd $GOPATH/src
go install github.com/mdempsky/gocode
go install github.com/uudashr/gopkgs/cmd/gopkgs
go install github.com/ramya-rao-a/go-outline
go install github.com/acroca/go-symbols
go install github.com/rogpeppe/godef
go install github.com/sqs/goreturns
go install github.com/derekparker/delve/cmd/dlv
go install github.com/cweill/gotests
go install github.com/josharian/impl
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
go install golang.org/x/lint/golint 

檢查插件是否安裝成功

        打開(最好重啟一下vs code) Visual Studio Code,在go文件中引入一個包,並寫出“包名.”會有方法提示,例如:我這里引入的是“math/rand”包,當我寫“rand”的時候會有下圖的提示,那就說明插件安裝成功了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM