golang-exercises/golangr/file-reader/main.go

34 lines
447 B
Go

package main
import (
"bufio"
"fmt"
"os"
)
var names = []string{}
var err error
func main() {
if len(os.Args) < 2 || len(os.Args) > 2 {
fmt.Println("Usage: ./file-reader names.txt")
os.Exit(1)
}
filePath := os.Args[1]
file, err := os.Open(filePath)
if err != nil {
fmt.Print(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
names = append(names, scanner.Text())
}
fmt.Println(names)
}