-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
66 lines (58 loc) · 1.74 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
/*
* Write a program which reads information from a file and represents it in a slice of structs.
* Assume that there is a text file which contains a series of names.
* Each line of the text file has a first name and a last name,
* in that order, separated by a single space on the line.
*
* Your program will define a name struct which has two fields,
* fname for the first name, and lname for the last name.
* Each field will be a string of size 20 (characters).
*
* Your program should prompt the user for the name of the text file.
* Your program will successively read each line of the text file
* and create a struct which contains the first and last names found in the file.
* Each struct created will be added to a slice, and after all lines have been read from the file,
* your program will have a slice containing one struct for each line in the file.
*
* After reading all lines from the file, your program should iterate through your slice of structs
* and print the first and last names found in each struct.
*/
type name struct {
fname string
lname string
}
func (n name) String() string {
return fmt.Sprintf("%v %v", n.fname, n.lname)
}
func main() {
var filename string
fmt.Print("Input path to the text file: ")
n, err := fmt.Scan(&filename)
if err != nil || n > 1 {
fmt.Println("invalid input")
return
}
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
nameList := make([]name, 0, 5)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
nameList = append(nameList, name{fields[0], fields[1]})
}
for _, v := range nameList {
fmt.Println(v)
}
}