09-go-type-system/questions/02-questions-defined-types.md
1-3: Yes, that's only one of the reasons.
// For example, let's say that you've defined a new type
// using time.Duration type like this:
type Millennium time.Duration
1: That's right. A defined type doesn't get its source type's methods.
2-4: Actually the defined type gets it from its underlying type.
var radius float32radius = type float32type radius float32 CORRECTtype radius = float321-2: This is not a correct syntax.
3:
radiusis a new type, defined usingfloat32.4: This declares
radiusas an alias tofloat32. So, they become the same types.
type Distance int
var (
village Distance = 50
city = 100
)
fmt.Print(village + city)
int(village + city)village + int(city)village(int) + cityvillage + Distance(city) CORRECT1-3: There's a type mismatch in this code. But, this won't fix it.
4: That's right. Now, the
city's type is Distance in the expression.
package main
import "fmt"
func main() {
celsius := 35.
fahrenheit := (9*celsius + 160) / 5
fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit)
}
1: But a degree value has a floating part. So, using an integer may not the best way.
2: float64 can represent a degree value.
3-4: But a degree value has a floating part. So, using an integer may not the best way. Also, there are two different temperature units here: Celsius and Fahrenheit. Isn't it better to create two distinct types?
type (
Duration int64
Century Duration
Millennium Century
)
int64 CORRECTDurationCentury1: That's right. Go's type system is flat. So, the defined type's underlying type is a type with a real structure. int64 is not just a name, it has its own structure, it's a predeclared type.
2: Duration is just a new type name. It doesn't have its own structure.
3: Century is just a new type name. It doesn't have its own structure.
HINT: Aliased types do not require type conversion.
1: byte data type is an alias to uint8 data type. So, they don't need conversion between each other. They're the same types.
HINT: Aliased types do not require type conversion.
3: rune data type is an alias to int32 data type. So, they don't need conversion between each other. They're the same types.