我在使用 VsCode 测试 Go 应用程序时遇到一些问题。这是我的 launch.json
{
"name": "Test",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/test",
"env": {},
"args": []
}
现在我遇到的问题是我的应用程序应该在子文件夹中写入文件(atm 是 ./temp)。为此,我有两个函数,第一个是确定文件路径
func getFilePath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
return dir + "/temp/ocicd-config.yaml"
}
另一个用于保存文件
func SaveToYaml(Config Structs.Project) {
fmt.Println("Saving Config")
yaml, err := yaml.Marshal(Config)
if err != nil {
panic(err)
}
fmt.Println(string(yaml))
ioutil.WriteFile(getFilePath(), yaml, 0644)
}
以及加载文件
func Load() Structs.Project {
fmt.Println("Loading Config")
file, err := ioutil.ReadFile(getFilePath())
if err != nil {
panic(err)
}
project := Structs.Project{}
err = yaml.Unmarshal(file, &project)
if err != nil {
panic(err)
}
return project
}
现在的问题是,VsCode 使应用程序在 ./test 子文件夹中运行,这使我的应用程序尝试从 ./test/temp 加载并保存到 ./test/temp,这不是我想要的。我尝试更改我的 launch.json 以实际使用 ${workspace} 作为程序并使用“./test”作为参数,但这会使测试完全停止工作。现在我很迷失。我有什么想法可以解决这个问题吗?
慕姐4208626
相关分类