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)
}
|
A A hat is initialized and assigned to the variable 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)
|
A type Dog struct {
breed string
color string
}
A Dog is initialized and assigned to the variable |
What does the following code print? type Phone struct {
phoneMake string
model string
}
myPhone := Phone{phoneMake: "iPhone", model: "7"}
fmt.Println(myPhone)
|
A |
What does the following code print? type Country struct {
name string
continent string
}
m := Country{"malta", "europe"}
fmt.Println(m.continent)
|
The 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)
|
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)
|
The The city is then updated to be |
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())
}
|
In the |
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)
}
|
The 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 Methods defined for the |