Skip to content

Instantly share code, notes, and snippets.

@jgsqware
Created September 18, 2018 07:52
Show Gist options
  • Save jgsqware/5d5c5892e9a5075fa19769b6703ce646 to your computer and use it in GitHub Desktop.
Save jgsqware/5d5c5892e9a5075fa19769b6703ce646 to your computer and use it in GitHub Desktop.
askForConfirmation in go
func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment