Skip to content

Instantly share code, notes, and snippets.

@clauswitt
Created November 24, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clauswitt/5a750a8fc2a5bb87aa1d to your computer and use it in GitHub Desktop.
Save clauswitt/5a750a8fc2a5bb87aa1d to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func main() {
lines, err := readLines("file.go")
if err != nil {
log.Fatalf("readLines: %s", err)
}
for i, line := range lines {
fmt.Println(i+1, line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment