如何使用VSCode中的Code Runner外掛執行golang程式碼
個人主頁 如何使用VSCode中的Code Runner 外掛執行golang程式碼
project的文件結如下
mylab
|---- main.go
|---- test.go
main.go
package main
func main() {
test()
}
test.go
package main
func test() {
println("in test")
}
在Code Runner外掛的Doc地址中
找到如下Configuration的配置說明
Configuration
Make sure the executor PATH of each language is set in the environment variable. You could also add entry into code-runner.executorMap to set the executor PATH. e.g. To set the executor PATH for ruby, php and html:
{
"code-runner.executorMap": {
"javascript": "node",
"php": "C:\\php\\php.exe",
"python": "python",
"perl": "perl",
"ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
"go": "go run",
"html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}
Supported customized parameters
- $workspaceRoot: The path of the folder opened in VS Code
- $dir: The directory of the code file being run
- $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash
- $fullFileName: The full name of the code file being run
- $fileName: The base name of the code file being run, that is the file without the directory
- $fileNameWithoutExt: The base name of the code file being run without its extension
- $driveLetter: The drive letter of the code file being run (Windows only)
- $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)
Please take care of the back slash and the space in file path of the executor
Back slash: please use \ If there ares spaces in file path, please use \" to surround your file path
You could set the executor per filename glob:
{
"code-runner.executorMapByGlob": {
"pom.xml": "cd $dir && mvn clean package",
"*.test.js": "tap",
"*.js": "node"
}
}
可以看到go的執行命令預設配置成了go run, 並且預設執行當前開啟的檔案。 所以需要利用$dir這個變數run整個main包所涉及到的所有檔案。並且可以正則匹配檔名。
檢視一下go run這個命令的引數解釋,由於眾所周知的原因,我們只能上國內的替代官網。
Compile and run Go program Usage:
go run [build flags] [-exec xprog] package [arguments...] Run compiles and runs the named main Go package. Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in 'go run .' or 'go run my/cmd'.
By default, 'go run' runs the compiled binary directly: 'a.out arguments...'. If the -exec flag is given, 'go run' invokes the binary using xprog:
'xprog a.out arguments...'. If the -exec flag is not given, GOOS or GOARCH is different from the system default, and a program named go$GOOS$GOARCH_exec can be found on the current search path, 'go run' invokes the binary using that program, for example 'go_nacl_386_exec a.out arguments...'. This allows execution of cross-compiled programs when a simulator or other execution method is available.
The exit status of Run is not the exit status of the compiled binary.
For more about build flags, see 'go help build'. For more about specifying packages, see 'go help packages'.
找到了,使用go run .
來跑整個package的。
在VSCode的.vscode目錄下建立settings.json檔案
自定義這個project的使用者配置如下
{
"code-runner.executorMap": {
"go": "cd $dir && go run .",
},
"code-runner.executorMapByGlob": {
"$dir\\*.go": "go"
}
}
點選Cdoe Runner在VSCode GUI介面提供的RUN按鈕
[Running] cd "d:\Users\polar\go\src\mylab\" && go run .
in test
[Done] exited with code=0 in 3.564 seconds
OK!
當然也可以用VSCode Debug 的launch.json來配置,但是那是debug模式,並不是go run。
歡迎轉載,請註明出處~ 作者個人主頁
相關文章
- VSCode 使用 Code Runner 外掛無法編譯執行檔名帶空格的檔案VSCode編譯
- 如何配置vscode外掛code runner編譯多個*.c檔案VSCode編譯
- Vs Code Batch-runner 外掛設定BAT
- Vscode外掛:將程式碼連線到IDE中的文件VSCodeIDE
- 【28】VsCode如何執行C#程式碼VSCodeC#
- 使用 VSCode Remote 外掛VSCodeREM
- vscode外掛使用包VSCode
- VSCode中類似Postman的外掛VSCodePostman
- VScode外掛 Rainbow安裝(程式碼彩虹屁神器)VSCodeAI
- HeyUI元件庫釋出vscode外掛,PS教程: 如何開發vscode外掛?UI元件VSCode
- VScode外掛VSCode
- win10 vscode 外掛使用Win10VSCode
- 解決vscode安裝golang外掛(windows)問題VSCodeGolangWindows
- 如何極狐GitLab Runner 使用特權身份執行Gitlab
- 使用Markdown建立PPT的VS Code外掛
- vscode自動註釋外掛的使用VSCode
- 歡迎使用園子的 vscode 外掛VSCode
- VScode 好用的外掛VSCode
- vs code 外掛
- vscode常用外掛VSCode
- vscode外掛分享VSCode
- vscode外掛整理VSCode
- 頂針的 VScode 外掛VSCode
- chatgpt-vscode:在VSCode中使用ChatGPT的擴充套件外掛ChatGPTVSCode套件
- 構建vscode的vue元件程式碼補全外掛以及上傳VSCodeVue元件
- Blockman:突出顯示ifelse等巢狀程式碼塊的Vscode外掛BloC巢狀VSCode
- VSCode 遠端開發外掛快速使用VSCode
- 教你如何在Golang中執行JavaScriptGolangJavaScript
- vscode中執行gulp的taskVSCode
- Maven外掛執行方式Maven
- VSCode中6個AI頂級外掛VSCodeAI
- VScode外掛推薦VSCode
- VSCode外掛之BeautifyVSCode
- VSCode 外掛測試VSCode
- vscode常用python外掛VSCodePython
- vscode 外掛配置指北VSCode
- VSCODE 外掛推薦VSCode
- vscode中隱藏節點模組的外掛VSCode