-
Notifications
You must be signed in to change notification settings - Fork 89
/
main.go
62 lines (54 loc) · 1.46 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
)
type student struct {
StudentId int `json:"id"`
LastName string `json:"lname"`
MiddleInitial string `json:"mname,omitempty"`
FirstName string `json:"fname"`
IsMarried bool `json:"-"`
IsEnrolled bool `json:",omitempty"`
Courses []course `json:"classes"`
}
type course struct {
Name string `json:"coursename"`
Number int `json:"coursenum"`
Hours int `json:"coursehours"`
}
func main() {
s := newStudent(1, "Williams", "s", "Felicia", false, false)
student1, err := json.MarshalIndent(s, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(student1))
fmt.Println()
s2 := newStudent(2, "Washington", "", "Bill", true, true)
c := course{Name: "World Lit", Number: 101, Hours: 3}
s2.Courses = append(s2.Courses, c)
c = course{Name: "Biology", Number: 201, Hours: 4}
s2.Courses = append(s2.Courses, c)
c = course{Name: "Intro to Go", Number: 101, Hours: 4}
s2.Courses = append(s2.Courses, c)
student2, err := json.MarshalIndent(s2, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(student2))
}
func newStudent(studentID int, lastName, middleInitial, firstName string,
isMarried, isEnrolled bool) student {
s := student{StudentId: studentID,
LastName: lastName,
MiddleInitial: middleInitial,
FirstName: firstName,
IsMarried: isMarried,
IsEnrolled: isEnrolled,
}
return s
}