Last active
March 21, 2020 17:55
-
-
Save mekilis/800aa8d310d6d8753a9c32f2a84e2465 to your computer and use it in GitHub Desktop.
Demo StdIO in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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