commit 6f04c2bef58552d59f02f33d7d2cc0c6502f842a Author: Gardient Date: Sat Nov 10 22:02:21 2018 +0200 initial commit diff --git a/panic.go b/panic.go new file mode 100644 index 0000000..7d61323 --- /dev/null +++ b/panic.go @@ -0,0 +1,8 @@ +package utils + +// PanicOnError will panic if err goven is not nil +func PanicOnError(err error) { + if err != nil { + panic(err) + } +} diff --git a/readline.go b/readline.go new file mode 100644 index 0000000..d540db7 --- /dev/null +++ b/readline.go @@ -0,0 +1,21 @@ +package utils + +import ( + "bufio" + "os" + "strings" +) + +var rd *bufio.Reader + +// ReadLine reads a line from stdin and strips newline from the end +func ReadLine() string { + if rd == nil { + rd = bufio.NewReader(os.Stdin) + } + + str, err := rd.ReadString('\n') + PanicOnError(err) + + return strings.TrimRight(str, "\r\n") +}