英文:
vs code debuging go tests not passing flags
问题
我正在尝试在VS Code上配置一个调试器来运行一些Go语言的测试。我需要传递一些标志参数,但是它没有正常工作。
main.go
package main
import (
"flag"
"fmt"
)
func DoTheThing() {
flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
flag.Parse()
fmt.Println(*flag1Ptr)
fmt.Println("Hello, world")
}
func main() {
DoTheThing()
}
main_test.go
package main
import "testing"
func TestDoTheThing(t *testing.T) {
DoTheThing()
}
launch.json
{
"name": "Launch app",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"-flag1"
]
},
{
"name": "Run Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"--", "-flag1"
]
}
如果我使用"Launch app"的配置来运行,它会正确地传递参数值,但是使用"Run Tests"的配置时,它不会设置该参数。
使用"Launch app"配置的输出结果:
true
Hello, world
使用"Run Tests"配置的输出结果:
false
Hello, world
英文:
I`m trying to config a debugger on vs-code to some tests in go. I have to pass some flags to it, but it's not working well.
main.go
package main
import (
"flag"
"fmt"
)
func DoTheThing() {
flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
flag.Parse()
fmt.Println(*flag1Ptr)
fmt.Println("Hello, world")
}
func main() {
DoTheThing()
}
main_test.go
package main
import "testing"
func TestDoTheThing(t *testing.T) {
DoTheThing()
}
launch.json
{
"name": "Launch app",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"-flag1"
]
},
{
"name": "Run Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"--", "-flag1"
]
}
if I run it with Launch app config it pass the value in the right way, but using the test one it do not set the parameter
output using Launch app config
true
Hello, world
output using Run Tests config
false
Hello, world
答案1
得分: 2
package main
import (
"flag"
"fmt"
)
var flag1Ptr *bool
func init(){
flag1Ptr = flag.Bool("flag1", false, "flag1 is a flag")
}
func DoTheThing() {
flag.Parse()
fmt.Println(*flag1Ptr)
fmt.Println("Hello, world")
}
func main() {
DoTheThing()
}
launch.json
{
"name": "Launch app",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"-flag1"
]
},
{
"name": "Run Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": ["-flag1"]
}
英文:
package main
import (
"flag"
"fmt"
)
var flag1Ptr *bool
func init(){
flag1Ptr = flag.Bool("flag1", false, "flag1 is a flag")
}
func DoTheThing() {
flag.Parse()
fmt.Println(*flag1Ptr)
fmt.Println("Hello, world")
}
func main() {
DoTheThing()
}
launch.json
{
"name": "Launch app",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"-flag1"
]
},
{
"name": "Run Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": ["-flag1"]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论