Skip to content

Instantly share code, notes, and snippets.

@yingzhuo
Created October 31, 2019 00:57
Show Gist options
  • Save yingzhuo/f08e50d87454dad1e85a838ef8e1f8d4 to your computer and use it in GitHub Desktop.
Save yingzhuo/f08e50d87454dad1e85a838ef8e1f8d4 to your computer and use it in GitHub Desktop.
load stdin #golang
func loadStdin() string {
info, err := os.Stdin.Stat()
if err != nil {
return ""
}
if info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0 {
return ""
}
reader := bufio.NewReader(os.Stdin)
var output []rune
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}
return string(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment