Skip to content

Instantly share code, notes, and snippets.

@birowo
Last active August 30, 2018 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save birowo/b74133bfa9b503781247c04f01a10615 to your computer and use it in GitHub Desktop.
Save birowo/b74133bfa9b503781247c04f01a10615 to your computer and use it in GitHub Desktop.
golang: membaca inputan user di terminal / stdin

stdinscaneof

di linux (saya pakai centos):

stdinreadallcentos

package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
)
func main() {
println("inputkan beberapa baris data, akhiri dengan Ctrl-Z lalu Enter")
result, _ := ioutil.ReadAll(os.Stdin)
newLine := []byte("\r\n")
println("cetak hasil :")
println(string(bytes.Replace(result, newLine, []byte(", "), -1)))
println("pecah hasil perbaris :")
//jika diinginkan inputan perbaris tinggal split result with separator \r\n
fmt.Printf("%q\n", bytes.Split(result, newLine))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment