Created
August 21, 2014 13:43
-
-
Save andreagrandi/544affe4567ba6f29f89 to your computer and use it in GitHub Desktop.
Read a text file and print its lines 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 ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"strings" | |
) | |
var fileName = flag.String("f", "", "Filename to be read") | |
func main() { | |
flag.Parse() | |
content, err := ioutil.ReadFile(*fileName) | |
if err != nil { | |
panic(err) | |
} | |
lines := strings.Split(string(content), "\n") | |
// First parameter returned by the next statement is a counter. | |
// If you don't need it, you use _ as return variable. | |
for _, line := range lines { | |
fmt.Println(line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: go run text_file_reader.go -f test.txt