在 VS Code 中新建一个文件,命名为 hello.go, 这时 VS Code 会自动提示需要安装一些 Go 语言相关的工具,选择安装。
VS Code 会按照一些列的工具,包括:
1 2 3 4 5 6 7 8 9 10 11
gocode for auto-completion (not needed if using language server) go-outline for symbol search in the current file go-symbols for symbol search in the current workspace gopkgs for auto-completion of unimported packages guru for the Find all References feature gorename for renaming symbols goreturns or goimports for formatting code (not needed if using language server) godef or gogetdoc for the Go to Definition feature (not needed if using language server) godoc or gogetdoc for the documentation that appears on hover (not needed if using language server) golint or gometalinter or megacheck or golangci-lint or revive for linting dlv for debugging
如果失败,你也可以在命令行窗口中执行以下命令来手动安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
go get -u -v github.com/ramya-rao-a/go-outline go get -u -v github.com/acroca/go-symbols go get -u -v github.com/mdempsky/gocode go get -u -v github.com/rogpeppe/godef go get -u -v golang.org/x/tools/cmd/godoc go get -u -v github.com/zmb3/gogetdoc go get -u -v golang.org/x/lint/golint go get -u -v github.com/fatih/gomodifytags go get -u -v golang.org/x/tools/cmd/gorename go get -u -v sourcegraph.com/sqs/goreturns go get -u -v golang.org/x/tools/cmd/goimports go get -u -v github.com/cweill/gotests/... go get -u -v golang.org/x/tools/cmd/guru go get -u -v github.com/josharian/impl go get -u -v github.com/haya14busa/goplay/cmd/goplay go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct go get -u -v github.com/alecthomas/gometalinter gometalinter --install
安装完成后,在 hello.go 文件中输入以下代码:
1 2 3 4 5 6 7 8 9
package main
import ( "fmt" )
funcmain() { fmt.Println("Hello, World!") }
然后在 VS Code 菜单中选择调试: Debug -> Start Debugging
如果在 Debug Console 窗口中看到输出以下内容:
1 2 3
API server listening at: 127.0.0.1:17972 Hello, World! Process exiting with code: 0