vscode Code Runner 无法运行多个 Go 文件。

huangapple go评论79阅读模式
英文:

vscode Code Runner cannot run multi go files

问题

例如,我有一个名为demo的简单项目

demo
├── go.mod
├── main.go
└── sum.go

以下是代码,对于go.mod,你可以在demo目录下运行go mod init来自动生成(但你也可以自己创建)

// main.go
package main

import "fmt"

func main() {
	num3 := sum(1, 2)
	fmt.Println(num3)
}

// sum.go
package main

func sum(num1 int, num2 int) int {
	return num1 + num2
}

// go.mod
module demo

go 1.17

现在,在main.go文件中,右键单击鼠标→运行代码,这意味着你将使用Code Runner运行main.go,但会打印一个错误

# command-line-arguments
demo/main.go:6:10: undefined: sum

这个错误的原因是Code Runner只运行main.go文件,如果我们在终端中cd到demo路径并运行go run .,代码就可以正常运行。

我们如何解决这个问题?

英文:

For example, I have a simple project called demo

demo
├── go.mod
├── main.go
└── sum.go

Here is the code, for go.mod, you can run go mod init under demo directory to generate automatically(but you can create in yourself to)

// main.go
package main

import "fmt"

func main() {
	num3 := sum(1, 2)
	fmt.Println(num3)
}

// sum.go
package main

func sum(num1 int, num2 int) int {
	return num1 + num2
}

// go.mod
module demo

go 1.17

Now in main.go file, right click mouse → Run Code, that means you will run main.go with Code Runner, but it will print an error

# command-line-arguments
demo/main.go:6:10: undefined: sum

The reason for this error is that Code Runner only runs the main.go file, if we cd to demo path in the Terminal and run go run ., the code can run very well.

How can we solve this problem?

答案1

得分: 1

如果我们想要使用Code Runner运行代码,我们应该添加一些配置,让Code Runner能够进入目标文件夹并运行"go run ."。

打开VSCode设置页面,点击右上角的"view json"按钮
vscode Code Runner 无法运行多个 Go 文件。

将以下配置添加到json中

"code-runner.executorMap": {
    "go": "cd $dir && go run .",
},
"code-runner.executorMapByGlob": {
    "$dir/*.go": "go"
},

请注意,设置json文件中可能已经有了"code-runner.executorMapByFileExtension",但这与"code-runner.executorMap"不同,请不要将"go": "cd $dir && go run ."添加到"code-runner.executorMapByFileExtension"中。

添加配置后,现在您可以使用Code Runner运行您的go代码,无需重新启动或重新加载VSCode。

英文:

If we want to run the code with Code Runner, we should add some config that can lets the Code Runner cd into the target folder and then run go run ..

open vscode setting page, click view json button on the top right corner
vscode Code Runner 无法运行多个 Go 文件。

Add the following config to the json

"code-runner.executorMap": {
    "go": "cd $dir && go run .",
},
"code-runner.executorMapByGlob": {
    "$dir/*.go": "go"
},

Be aware that the settings json may already have "code-runner.executorMapByFileExtension", but this is not the same with "code-runner.executorMap", do not add "go": "cd $dir && go run .", to "code-runner.executorMapByFileExtension".

After adding the config, now you can run your go code with Code Runner, you don't need restart or reload vscode.

huangapple
  • 本文由 发表于 2022年1月26日 15:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/70859607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定