自從 Windows 升級到 Windows 10 Version 2004 之後,因為可以採用效能大幅提昇的 WSL 2 執行環境,像是 .NET Core、Go 與 Docker 都會全部跑在 WSL 2 的 Linux 下。今天這篇文章,我將介紹如何在 Windows 平台打造完美的 Go 開發環境,不但可以讓你享受 Linux 平台執行時無比的順暢,更能享受 Windows 平台絕佳的 VSCode 開發體驗。
自從 Windows 升級到 Windows 10 Version 2004 之後,因為可以採用效能大幅提昇的 WSL 2 執行環境,像是 .NET Core、Golang 與 Docker 都會全部跑在 WSL 2 的 Linux 下。今天這篇文章,我將介紹如何在 Windows 平台打造完美的 Golang 開發環境,不但可以讓你享受 Linux 平台執行時無比的順暢,更能享受 Windows 平台絕佳的 VSCode 開發體驗。
使用 WSL 在 Windows 上安裝 Linux
開發人員可以在 Windows 計算機上同時訪問 Windows 和 Linux 的強大功能。通過適用於 Linux 的 Windows 子系統(WSL),開發人員可以安裝 Linux 發行版(例如Ubuntu、OpenSUSE、Kali、Debian、Arch Linux 等),並直接在 Windows 上使用 Linux 應用程序、實用程序和 Bash 命令行工具,不用進行任何修改,也無需承擔傳統虛擬機或雙啟動設置的費用。
安裝 WSL 命令
現在,可以使用單個命令安裝運行 WSL 所需的一切內容。在管理員模式下打開 PowerShell 或 Windows 命令提示符,方法是右鍵單擊並選擇"以管理員身份運行",輸入 wsl --install 命令,然後重啟電腦。
如果有遇到安裝 gcc 時報 404 Not Found 的錯誤,請先升級你的 apt 後再進行安裝:
錯誤訊息:
1
2
3
4
5
E:Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/b/binutils/libctf0_2.34-6ubuntu1.1_amd64.deb 404 Not Found [IP: 91.189.88.152 80]E:Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/b/binutils/binutils-x86-64-linux-gnu_2.34-6ubuntu1.1_amd64.deb 404 Not Found [IP: 91.189.88.152 80]E:Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/b/binutils/binutils_2.34-6ubuntu1.1_amd64.deb 404 Not Found [IP: 91.189.88.152 80]E:Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/l/linux/linux-libc-dev_5.4.0-81.91_amd64.deb 404 Not Found [IP: 91.189.88.152 80]E:Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
gvm pkgset
# * http://github.com/moovweb/gvm# == Description# GVM pkgset is used to manage various Go packages# == Usage# gvm pkgset Command# == Command# create - create a new package set# delete - delete a package set# use - select where gb and goinstall target and link# empty - remove all code and compiled binaries from package set# list - list installed go packages
go get github.com/sqs/goreturns
go get github.com/mdempsky/gocode
go get github.com/uudashr/gopkgs/v2/cmd/gopkgs
go get github.com/ramya-rao-a/go-outline
go get github.com/acroca/go-symbols
go get golang.org/x/tools/cmd/guru
go get golang.org/x/tools/cmd/gorename
go get github.com/cweill/gotests/...
go get github.com/fatih/gomodifytags
go get github.com/josharian/impl
go get github.com/davidrjenni/reftools/cmd/fillstruct
go get github.com/haya14busa/goplay/cmd/goplay
go get github.com/godoctor/godoctor
go get github.com/go-delve/delve/cmd/dlv
go get github.com/stamblerre/gocode
go get github.com/rogpeppe/godef
go get golang.org/x/tools/cmd/goimports
go get golang.org/x/lint/golint
go get golang.org/x/tools/gopls
啟動 Visual Studio Code 開發工具
你現在可以在任何 WSL Distro 裡面直接輸入 code . 就可以開啟 Windows 環境下的 Visual Studio Code 開發工具!
{// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version":"2.0.0","tasks":[{"label":"build","command":"go","type":"shell","args":["build","-o","dist/${workspaceFolderBasename}"],"group":{"kind":"build","isDefault":true},"presentation":{"reveal":"silent"},"problemMatcher":"$msCompile"},{"label":"test","command":"go","type":"shell","args":["test",],"group":{"kind":"test","isDefault":true},"presentation":{"reveal":"silent"},"problemMatcher":"$msCompile"}]}