Skip to content

Instantly share code, notes, and snippets.

@k4ml
Last active October 14, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4ml/ee9129d5dbf7ec06e8115a1d8b127905 to your computer and use it in GitHub Desktop.
Save k4ml/ee9129d5dbf7ec06e8115a1d8b127905 to your computer and use it in GitHub Desktop.
Reading input from console in Go
// https://tutorialedge.net/golang/reading-console-input-golang/
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Simple Shell")
fmt.Println("---------------------")
for {
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
if text == "\\q" {
os.Exit(0)
}
if strings.Compare("hi", text) == 0 {
fmt.Println("hello, Yourself")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment