我正在尝试从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)
}
答案 0 :(得分:1)
ReadString('\n')返回定界符'\ n',您需要将其删除。
featureName, err := reader.ReadString('\n')
if err == nil {
n := len(featureName)
featureName = featureName[:n-1]
}
...