Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道您可以通过执行以下操作来调用用户的文本输入:
fmt.Print("Enter text: ") reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n')
这将输出:
Enter text:
但是有没有办法为这个输入设置一个用户可以编辑的初始值。例如,如果初始值设置为“english”,那么输出将是:
Enter text: english
我的建议是将默认值放在提示符中:
def := "english" fmt.Printf("Enter text (%s): ", def) reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') text = strings.TrimSuffix(text, "\n") if text == "" { text = def }