24-structs/questions/README.md
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.
2: Yes, each field should have a unique name. Also, each field should have a type, but every field can have a different type.
type weather struct {
temperature, humidity float64
windSpeed float64
temperature float64
}
temperature, humidity float64 field is a syntax errortemperature field is not unique CORRECT2: That's a parallel definition. It defines two float64 fields: temperature and humidity. It is correct.
3: Right! Struct field names should be unique.
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.
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.
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,
}
2: When creating a struct value, it doesn't matter whether you use the field names or not. So, they are equal.
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)
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.
type item struct { title string }
type movie struct { item }
type performance struct { item }
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.
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)
4: Right!
m.titlereturns "avengers: end game" because the outer type always takes priority. However,m.item.titlereturns "midnight in paris" because you explicitly get it from the inner type: item.
4: Correct. For example, the json package can read and encode/decode depending on the associated metadata.
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.
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 CORRECT1: 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.
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.