When should you use a struct type?
- For storing the same type of values
- For adding an additional type of values in runtime
- For combining different types in a single type to represent a concept CORRECT
1: Arrays, slices, and maps are better candidates for that.
2: Struct fields are fixed at compile-time, you cannot add additional fields in runtime, neither you can remove them.
3: That's right. A struct type combines different types of fields in a single type. You can use a struct type to represent a concept.
What are the properties of struct fields?
- They all should be of the same type
- Each one should have a name and possibly a different type CORRECT
- You can add additional fields in runtime
- You can remove the existing fields in runtime
2: Yes, each field should have a unique name. Also, each field should have a type, but every field can have a different type.
What is wrong with the following code?
type weather struct {
temperature, humidity float64
windSpeed float64
temperature float64
}
- Nothing is wrong with it
temperature, humidity float64
field is a syntax errortemperature
field is not unique CORRECT
2: That's a parallel definition. It defines two float64 fields: temperature and humidity. It is correct.
3: Right! Struct field names should be unique.
What is the zero-value of the following struct value?
var movie struct {
title, genre string
rating float64
released bool
}
{}
{title: "", genre: "", rating: 0, released: false}
CORRECT{title: "", genre: "", rating: 0, released: true}
{"title, genre": "", rating: 0, released: false}
1: That's an empty struct value with no fields.
2: Right! Go initializes a struct's fields to zero-values depending on their type.
What is the type of the following struct?
avengers := struct {
title, genre string
rating float64
released bool
}{
"avengers: end game", "sci-fi", 8.9, true,
}
fmt.Printf("%T\n", avengers)
struct{}
struct{ string; string; float64; bool }
struct{ title string; genre string; rating float64; released bool }
CORRECT{title: "avengers: end game"; genre: "sci-fi"; rating: 8.9; released: true}
1: That's an empty struct type with no fields.
2: Fields names is also a part of a struct's type.
3: Right! Field names and types are part of a struct's type.
4: Nope, that's a struct value.
Are the following struct values equal?
type movie struct {
title, genre string
rating float64
released bool
}
avengers := movie{"avengers: end game", "sci-fi", 8.9, true}
clone := movie{
title: "avengers: end game", genre: "sci-fi",
rating: 8.9, released: true,
}
- There is a syntax error
- Yes CORRECT
- No
2: When creating a struct value, it doesn't matter whether you use the field names or not. So, they are equal.
Are the following struct values equal? If not, why?
type movie struct {
title, genre string
rating float64
released bool
}
avengers := movie{
title: "avengers: end game", genre: "sci-fi",
rating: 8.9, released: true,
}
clone := movie{title: "avengers: end game", genre: "sci-fi"}
fmt.Println(avengers == clone)
- Yes: They have the same set of fields
- No : They are not comparable
- No : Field values are different CORRECT
1: That's right, this means they are comparable, but that's not enough.
2: Yes, they are. They use the same struct type.
3: Yes, when you omit some of the fields, Go assigns zero values to them. Here, "clone" struct value's "rating" and "released" fields are: 0, and false, respectively.
Do the movie and performance struct types have the same types?
type item struct { title string }
type movie struct { item }
type performance struct { item }
- Yes: They have the same set of fields
- No : They have different type names CORRECT
- No : An embedded field cannot be compared
2: Right! Types with different names cannot be compared. However, you can convert one of them to the other because they have the same set of fields. movie{} == movie(performance{}) is ok, or vice versa.
What does the program print?
type item struct{ title string }
type movie struct {
item
title string
}
m := movie{
title: "avengers: end game",
item: item{"midnight in paris"},
}
fmt.Println(m.title, "&", m.item.title)
- midnight in paris & midnight in paris
- avengers: end game & avengers: end game
- midnight in paris & avengers: end game
- avengers: end game & midnight in paris CORRECT
4: Right!
m.title
returns "avengers: end game" because the outer type always takes priority. However,m.item.title
returns "midnight in paris" because you explicitly get it from the inner type: item.
What is a field tag?
- It allows Go to index struct fields more efficiently
- You can use it for documenting your code
- It's like a comment
- Associates metadata about the field CORRECT
4: Correct. For example, the json package can read and encode/decode depending on the associated metadata.
Which one is correct about a field tag?
- It needs to be typed according to some rules
- You can change it to a different value in runtime
- It's just a string value, and it doesn't have a meaning on its own CORRECT
1: This is true to some extent but it can have any value.
2: Fields tags are part of a struct type definition so you cannot change their value in runtime.
3: Right! It's just a string value. It's only meaningful when other code reads it. For example, the json package can read it and encode/decode depending on the field tag's value.
What is wrong with the following program?
type movie struct {
title string `json:"title"`
}
m := movie{"black panthers"}
encoded, _ := json.Marshal(m)
fmt.Println(string(encoded))
movie
is unexported so you cannot encodetitle
is unexported so you cannot encode CORRECT- Error handling is missing so you cannot encode
1: The json package can encode a struct even though its type is unexported.
2: Right! The json package can only encode exported fields.
3: It's better to handle errors but it's not the main problem here.
Why do you need to pass a pointer to the Unmarshal function?
- To make it work faster and efficient
- So it can update the value on memory CORRECT
- To prevent errors
2: Otherwise, it would not be able to update the given value. It's because, every value in Go is passed by value. So a function can only change the copy, not the original value. However, through a pointer, a function can change the original value.