Golang 學習心得(VSCode) === ###### tags: `Golang` `go` `VSCode` `程式語言` `Programming language` --- # 📕 安裝 首先下載安裝檔 https://golang.org/dl/ 安裝完輸入(基本上不會設錯) ```bash= go env ``` 看看 GOROOT="C:\Go" 與 GOPATH="C:\users\youName\go" 是否正確(基本上不會設錯) --- # 📕 安裝 vscode 的 Go 語言包   # 📕 建立 main.go 檔案 輸入程式碼 ```go= package main import "fmt" func main() { fmt.Println("Hello World!") } ``` 必須再輸入指令 ```shell= "專案名稱" go mod init "專案名稱" ``` 會產生一個 go.mod 不然偵錯時,會出現錯誤,如下:  可以順便輸入編譯(沒編譯也可以偵錯): ```shell= go build ``` 會產生一個執行檔案 * mac 環境下 go build 產生錯誤 ```go= # golang.org/x/sys/unix ../../../go/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/syscall_darwin.1_13.go:25:3: //go:linkname must refer to declared function or variable .... ../../../go/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3: //go:linkname must refer to declared function or variable ../../../go/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3: too many errors ``` * 解決方法 ```bash= go get -u golang.org/x/sys ``` --- 如果選字 fmt. 之後沒有出來 會提示要安裝 go 的套件 都同意吧 install all 安裝完就能正常選字了 --- # 📕 安裝一下 ==code runer==  在 main.go 檔案點擊右鍵上「Run Code 」 就能執行程式碼了 如果不能下中斷 刪除目錄 ```bash= C:\Users(User)\go\src\github.com\go-delve ``` 重新執行 ```bash= go get -u github.com/go-delve/delve/cmd/dlv ``` 或許能正常(我的執行完正常了) 下面指令可以驗證 dlv 是否安裝成功 ```bash= dlv version ``` :::warning * mac 發生錯誤,不能中斷 ```bash= zsh: command not found: dlv ``` 開啟 Xcode 的開發者模式?(不管怎樣,我開了) ```bash= sudo /usr/sbin/DevToolsSecurity -enable ``` ```bash= vim ~/.zshrc ``` 新增下面路徑: (PATH一定要設定) ```bash= export GOROOT="/usr/local/go" export GOPATH="/Users/sam_liaw/go" export PATH="$GOPATH/bin:$PATH" ``` 重置設定: ```bash= source ~/.zshrc ``` ```bash= Ensure your VSCode uses the arm64 version. (it can use a different go version from the system) Run Go: install/update tools. It will rebuild all tools with the arm64 go version. ``` * 中斷就發生錯誤 ```bash= ERROR: protocol error E74 during registers read for packet $g;thread:5c48; ``` 重新安裝 xcode: ```bash= sudo rm -rf /Library/Developer/CommandLineTools xcode-select --install ``` 重新安裝go與dlv: ```base sudo rm -rvf /usr/local/go/ sudo rm -rvf /Users/sam_liaw/go/bin/dlv ``` 安裝 dlv: ```bash= git clone https://github.com/go-delve/delve.git cd delve/cmd/dlv/ go build go install ``` ==沒用== ```bash= brew install dlv ``` ==最後竟然是brew安裝後就正常了== ::: # 📕 launch.json ```json= { // 使用 IntelliSense 以得知可用的屬性。 // 暫留以檢視現有屬性的描述。 // 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "golang", "type": "go", "request": "launch", "mode": "auto", //當執行單個檔案時{workspaceFolder}可改為{file} "program": "${workspaceFolder}", "env": {}, "args": [] } ] } ``` # 📕 go formatter [VScode go代码风格格式化设置](https://blog.csdn.net/a344288106/article/details/109513887) * settings.json 找關鍵字 format On Save ```json= "[go]": { "editor.insertSpaces": false, "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true }, "editor.suggest.snippetsPreventQuickSuggestions": false } ``` # 📕 參考資料 [Day 29: 使用 VS Code 來開發 Go](https://ithelp.ithome.com.tw/articles/10228420) [Problem: Installation(repo) error on windows](https://github.com/go-delve/delve/issues/1509) # 📕 強烈推薦 [VSCode 必裝套件](https://hackmd.io/@sam-liaw/BJnLhni7U) # 📕 還沒讀完 [Go into Web! 系列](https://ithelp.ithome.com.tw/users/20120647/ironman/3110) 跟著菜鳥工程師一起學習如何使用 Golang 開發並結合 Docker 建構一個可上線的 web service [下班加減學點Golang與Docker 系列](https://ithelp.ithome.com.tw/users/20104930/ironman/2647) 介紹些自己這兩三個月學的Go與Docker的部份 藉由這三十天重新審視自己的概念
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up