-
Notifications
You must be signed in to change notification settings - Fork 8
/
read.go
48 lines (41 loc) · 834 Bytes
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Person struct {
fname string
lname string
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter file name:")
scanner.Scan()
fileName := scanner.Text()
var fileHandler *os.File
slice := make([]Person, 0, 1)
for {
fi, err := os.Open(fileName)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
fmt.Println("Please Enter valid file name:")
scanner.Scan()
fileName = scanner.Text()
} else {
fileHandler = fi
break
}
}
fileScanner := bufio.NewScanner(fileHandler)
var arr []string
for fileScanner.Scan() {
arr = strings.Split(fileScanner.Text(), " ")
slice = append(slice, Person{arr[0], arr[1]})
}
fileHandler.Close()
for _, person := range slice {
fmt.Printf("%v - %v\n", person.fname, person.lname)
}
}