Skip to content

Instantly share code, notes, and snippets.

@sgen
Created July 27, 2018 18:56
Show Gist options
  • Save sgen/f1a8ba864ad4b895b005205db0ca3023 to your computer and use it in GitHub Desktop.
Save sgen/f1a8ba864ad4b895b005205db0ca3023 to your computer and use it in GitHub Desktop.
A basic prompt function in go
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