Created
September 18, 2018 07:52
-
-
Save jgsqware/5d5c5892e9a5075fa19769b6703ce646 to your computer and use it in GitHub Desktop.
askForConfirmation in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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