forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2.go
45 lines (36 loc) · 1.26 KB
/
exercise2.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
// 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/oEtveMoO1s
// Declare a struct type and create a value of this type. Declare a function
// that can change the value of some field in this struct type. Display the
// value before and after the call to your function.
package main
import "fmt"
// user represents a user in the system.
type user struct {
name string
email string
accessLevel int
}
// main is the entry point for the application.
func main() {
// Create a variable of type user and initialize each field.
bill := user{
name: "Bill",
email: "bill@ardanlabs.com",
accessLevel: 1,
}
// Display the value of the accessLevel field.
fmt.Println("access:", bill.accessLevel)
// Share the bill variable with the accessLevel function
// along with a value to update the accessLevel field with.
accessLevel(&bill, 10)
// Display the value of the accessLevel field again.
fmt.Println("access:", bill.accessLevel)
}
// accessLevel changes the value of the users access level.
func accessLevel(u *user, accessLevel int) {
// Set of value of the accessLevel field to the value
// that is passed in.
u.accessLevel = accessLevel
}