Skip to content

Instantly share code, notes, and snippets.

@yoseplee
Created June 1, 2020 02:07
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 yoseplee/fe862d4a5afff361a6961fde1ac4b680 to your computer and use it in GitHub Desktop.
Save yoseplee/fe862d4a5afff361a6961fde1ac4b680 to your computer and use it in GitHub Desktop.
a code snippet for reading stdin in golang

Read stdin in golang

func read() {
  reader := bufio.NewReader(os.Stdin)
  fmt.Println("Enter your text: ")
  text, _ := reader.ReadString('\n')
  fmt.Println(text)
}

parse into cmd and argv

func parse(raw string) (cmd string, argv []string) {
	replaced := strings.Replace(raw, "\n", "",  -1)
	split := strings.Split(replaced, " ")
	cmd = split[0]
	argv = split[0:]
	return cmd, argv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment