Created
July 27, 2018 18:56
-
-
Save sgen/f1a8ba864ad4b895b005205db0ca3023 to your computer and use it in GitHub Desktop.
A basic prompt function in go
This file contains 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
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
// prompt prompts the user for input, printing p to stdout and reading from | |
// stdin until a newline is read. If no value is read from stdin before a | |
// newline is found d is returned. | |
func prompt(p string, d string) string { | |
fmt.Fprintf(os.Stdout, "%s: ", p) | |
var input string | |
fmt.Fscanln(os.Stdin, &input) | |
if len(input) == 0 { | |
input = d | |
} | |
return input | |
} | |
func main() { | |
input := prompt("Please provied some input", "default value") | |
fmt.Println(input) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment