Structs and Methods

Question Click to View Answer

What does the following code print?

package main

import "fmt"

func main() {
    type Hat struct {
        color string
    }

    h := Hat{"blue"}
    fmt.Println(h)
}

{blue} is printed.

A Hat struct is defined.

A hat is initialized and assigned to the variable h.

The struct is printed to the console.

What does the following code print?

type Dog struct {
    breed, color string
}

fido := Dog{"pug", "tan"}
fmt.Println(fido)

{pug tan} is printed.

A Dog struct is defined with multiple fields of the same type collapsed on a single line. We could also have defined the Dog struct like this.

type Dog struct {
    breed string
    color string
}

A Dog is initialized and assigned to the variable fido.

What does the following code print?

type Phone struct {
    phoneMake string
    model string
}

myPhone := Phone{phoneMake: "iPhone", model: "7"}
fmt.Println(myPhone)

{iPhone 7} is printed.

A Phone struct is defined and then a phone is initialized with named arguments.

What does the following code print?

type Country struct {
    name string
    continent string
}

m := Country{"malta", "europe"}
fmt.Println(m.continent)

europe is printed.

The Country struct is defined with a name and a continent.

We initialize a country and then use dot notation to access the country field of the struct.

What does the following code print?

type BestNumber struct {
    num1 int
    num2 int
}
var b BestNumber
fmt.Println(b.num2)

0 is printed.

When a struct is initialized without any arguments, the zero values are used for each of the fields. The zero value for an integer is zero.

What does the following code print?

type Restaurant struct {
    foodType string
    city string
}
r := Restaurant{"mexican", "miami"}
r.city = "denver"
fmt.Println(r)

{mexican denver} is printed.

The Restaurant struct is initally initialized with a foodType of "mexican" and a city of "miami".

The city is then updated to be "denver".

What does the following code print?

type Person struct {
    firstName string
    lastName string
}

func (p Person) fullName() string {
  return p.firstName + " " + p.lastName
}

func main() {
    coolGuy := Person{"napoleon", "dynamite"}
    fmt.Println(coolGuy.fullName())
}

napoleon dynamite is printed.

fullName() is a method defined on the Person struct.

In the main() function, we initialize a person and invoke the fullName() method.

What does the following code print?

type Cat struct {
    color string
}

func (c Cat) changeColor() {
  c.color = "pink"
}

func main() {
    funCat := Cat{"black"}
    funCat.changeColor()
    fmt.Println(funCat.color)
}

black is printed.

The changeColor() method does not actually change the color of the cat!

We'll discuss why this is the case and how to actually mutate a struct in a method in a future quiz.

What does the following code print?

type Person struct { 
    name string
}

func (p Person) Talk() string { 
    return "My name is " + p.name
}

type SoccerPlayer struct { 
    Person
    lifeGoal string
}

func (s SoccerPlayer) Inspire() string { 
    return "My life goal is to " + s.lifeGoal
}

func main() {
    rose := Person{"rose"}
    s := SoccerPlayer{rose, "win a world cup"}
    fmt.Println(s.Talk())
    fmt.Println(s.Inspire())
}
My name is rose
My life goal is to win a world cup

This code defines Person and SoccerPlayer structs. A person "is a" soccer player and Go supports these relationships with embedded types. Person is added as an anonymous field in the SoccerPlayer struct.

Methods defined for the Person struct can be invoked directly on SoccerPlayer structs. We can call the Talk() method with s.Talk() or s.Person.Talk().