-
Notifications
You must be signed in to change notification settings - Fork 2
/
exercise2.go
49 lines (38 loc) · 1.1 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
46
47
48
49
// Copyright 2014 Ardan Studios
//
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// 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"
// Add imports.
// Declare a type named user.
type user struct {
name string
email string
age int
}
// Create a function that changes the value of one of the user fields.
func updateAge(u *user, age int) {
// Use the pointer to change the value that the
// pointer points to.
u.age = age
}
func main() {
// Create a variable of type user and initialize each field.
u := user{
name: "John",
email: "john@foo.bar",
age: 13,
}
// Display the value of the variable.
fmt.Println("Name:", u.name)
fmt.Println("Email:", u.email)
fmt.Println("Age:", u.age)
// Share the variable with the function you declared above.
updateAge(&u, 15)
// Display the value of the variable.
fmt.Println("Age:", u.age)
}