英文:
"exit status 128" trying to create git branches from golang
问题
我正在尝试从Golang创建一个“特性分支”,以下代码无法正常工作:
reader := bufio.NewReader(os.Stdin)
fmt.Print(color.RedString("New feature description: "))
featureName, _ := reader.ReadString('\n')
featureName = strings.ReplaceAll(featureName, " ", "-")
featureBranchName := "feature/" + featureName
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", featureBranchName}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
但是,如果硬编码featureBranchName
,它可以正常工作:
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", "feature/ciaone-proprio"}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
请注意,这是一段Golang代码,用于从标准输入读取新特性描述,并根据描述创建一个特性分支。第一个代码段中的问题可能是featureName
中包含了换行符\n
,导致分支名称无效。
英文:
I am trying to create a "feature branch" from golang. The following code is not working:
reader := bufio.NewReader(os.Stdin)
fmt.Print(color.RedString("New feature description: "))
featureName, _ := reader.ReadString('\n')
featureName = strings.ReplaceAll(featureName, " ", "-")
featureBranchName := "feature/" + featureName
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", featureBranchName}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
But hardcoding featureBranchName
it works:
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", "feature/ciaone-proprio"}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
答案1
得分: 1
[ReadString('\n')]返回分隔符'\n',你需要将其移除。
featureName, err := reader.ReadString('\n')
if err == nil {
n := len(featureName)
featureName = featureName[:n-1]
}
...
英文:
[ReadString('\n')][1] returns the delimiter '\n', you need to remove it.
featureName, err := reader.ReadString('\n')
if err == nil {
n := len(featureName)
featureName = featureName[:n-1]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论