-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
54 lines (49 loc) · 1.07 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
package main
import (
"fmt"
"os"
"strings"
)
type name struct {
fname string
lname string
}
func main() {
//Place names.txt in same directory (format: firstname lastname)
// opening a file
fmt.Println("Enter file name which consists names:")
var fileName string
fmt.Scan(&fileName)
f, error := os.Open(fileName)
//finding file size
fi, statErr := f.Stat()
var content []byte
if statErr == nil && error == nil {
content = make([]byte, fi.Size())
//reading file content
nb, readErr := f.Read(content)
if readErr == nil && nb > 0 {
//fmt.Print(string(content), "\n")
} else {
fmt.Print(readErr)
}
} else {
fmt.Print(error)
}
//Core logic
strs := strings.Split((string(content)), "\n")
var n name
var nstr []string
sli := make([]name, len(strs), len(strs)+1)
for i := 0; i < len(strs); i++ {
nstr = strings.Split(strs[i], " ")
n.fname = nstr[0]
n.lname = nstr[1]
sli[i] = n
}
// Printing fname and lname from slice of structs
for i := 0; i < len(sli); i++ {
fmt.Println("Fname = :", sli[i].fname)
fmt.Println("Lname = :", sli[i].lname)
}
}