Skip to content

Instantly share code, notes, and snippets.

@mekilis
Last active March 21, 2020 17:55
Show Gist options
  • Save mekilis/800aa8d310d6d8753a9c32f2a84e2465 to your computer and use it in GitHub Desktop.
Save mekilis/800aa8d310d6d8753a9c32f2a84e2465 to your computer and use it in GitHub Desktop.
Demo StdIO in Go
package main
import (
"fmt"
"strings"
)
func main() {
// assuming user inputs 10, 5, 7
var i, j int
var s string
r := strings.NewReader("10 5 7")
fmt.Fscan(r, &i) // reads just 10
fmt.Print(i) // print result without a new line
fmt.Fscanf(r, "%d %d", &i, &j) // reads 5 and 7 next
fmt.Printf(", %d, %d\n", i, j) // seperate result with comma and then print a new line C-style
r = strings.NewReader("10-5-7")
fmt.Fscanln(r, &s) // reads all three numbers (without spaces) as text
fmt.Println(s) // output all three as text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment