forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
50 lines (42 loc) · 1.16 KB
/
exercise1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// http://play.golang.org/p/rZH_5xLAaK
// Declare a struct type to maintain information about a user (name, email and age).
// Create a value of this type, initialize with values and display each field.
//
// Declare and initialize an anonymous struct type with the same three fields. Display the value.
package main
import "fmt"
// user represents a user in the system.
type user struct {
name string
email string
age int
}
// main is the entry point for the application.
func main() {
// Declare variable of type user and init using a struct literal.
bill := user{
name: "Bill",
email: "bill@ardanlabs.com",
age: 45,
}
// Display the field values.
fmt.Println("Name", bill.name)
fmt.Println("Email", bill.email)
fmt.Println("Age", bill.age)
// Declare a variable using an anonymous struct.
ed := struct {
name string
email string
age int
}{
name: "Ed",
email: "ed@ardanlabs.com",
age: 46,
}
// Display the field values.
fmt.Println("Name", ed.name)
fmt.Println("Email", ed.email)
fmt.Println("Age", ed.age)
}