-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
46 lines (43 loc) · 770 Bytes
/
app.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
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
type Person struct {
State string `json:"State"`
City string `json:"City"`
Fullname string `json:"FullName"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Missing parameter")
return
}
csvFile, err := os.Open(os.Args[1])
file := csv.NewReader(bufio.NewReader(csvFile))
if err != nil {
fmt.Println("Can't read file")
panic(err)
}
var people []Person
for {
row, error := file.Read()
if error == io.EOF {
break
} else if error != nil {
log.Fatal(error)
}
people = append(people, Person{
State: row[0],
City: row[1],
Fullname: row[2],
})
}
peopleJson, _ := json.Marshal(people)
fmt.Println(string(peopleJson))
}