-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
47 lines (42 loc) · 1.22 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
)
// name struct has two fields, fname for the first name, and lname for the last name
type name struct {
fname string
lname string
}
func read() {
/**
* Create a txt file with space separated first and last name only. Each line should contain one name only.
* The name of the file should not be more than 1 word, suffixed by .txt extension.
*/
var filename string
fmt.Print("Enter the file name(e.g.: names.txt): ")
fmt.Scan(&filename)
// reading the file
dat, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(errors.New("Something went wrong : FILE NOT FOUND"))
} else if !strings.HasSuffix(filename, ".txt") {
fmt.Println(errors.New("Something went wrong : ONLY .txt FILE SUPPORTED"))
} else {
// creating a slice of struct
names := []name{}
strdat := strings.Split(string(dat), "\n")
for _, value := range strdat {
var tempName name
tempName.fname = strings.Split(value, " ")[0]
tempName.lname = strings.Split(value, " ")[1]
names = append(names, tempName)
}
fmt.Println("File reading complete...\nPrinting the names...")
for i, v := range names {
fmt.Printf("Name %d: %s %s\n", i+1, v.fname, v.lname)
}
}
}