I have stumbled upon this type alias in code:
type LightSource = struct {
R, G, B, L float32
X, Y, Z, A float32
//...
}
My question is: what would be the reason to use a type alias like that to define a struct
, rather than doing this?
type LightSource struct {
R, G, B, L float32
//...etc
}
Type aliasing refers to the technique of providing an alternate name for an existing type. Type aliasing was introduced in Go version 1.9 and above. This helps to facilitate code refactor for large codebases.
Type aliases Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead. It's useful to shorten long generic types. For instance, it's often tempting to shrink collection types: typealias NodeSet = Set<Network.
Type is the base interface for all data types in Go. This means that all other data types (such as int , float , or string ) implement the Type interface. Type is defined in the reflect header.
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T 's underlying type is the underlying type of the type to which T refers in its type declaration.
In this case I assume this was done by mistake - the first alternative defines a type "anonymously" and then assigns an alias to it, so the end result is the same in both cases, but still the second alternative is the only correct one.
Type aliases are useful only in a few cases, e.g. as written here, they can be useful for large-scale refactoring.
I find type aliases useful for readability. For example, in tests you might compare the output of the JSON decoder like this:
reflect.DeepEqual(r, map[string]interface{}{"a": map[string]interface{}{"b": 42.0}})
but you can use a type alias to improve the readability:
type JsonObject = map[string]interface{}
...
reflect.DeepEqual(r, JsonObject{"a": JsonObject{"b": 42.0}})
Because DeepEqual uses reflection to compare the types (and values) then making the type alias a type definition (by removing the = character) will cause DeepEqual to fail. You can try it in the Go Playground
To add to Rob64's answer, he states that the end result is the same, but do not mistake the fact that you'll end up with two different types.
type LightSource = struct { // Type struct
R, G, B, L float32
X, Y, Z, A float32
//...
}
type LightSource struct { // Type LightSource
R, G, B, L float32
//...etc
}
I feel that there's a lot of confusion in the fact that you'll end up with a different type, this means that if somewhere in your code you enforce a type A, and you use the wrong way of type aliasing, you'll end up with absolutely zero restriction on your "type alias"; the end-goal would really be to differentiate your types if you do end up creating a type alias, but that's obviously void if you make this mistake.
Go play<<
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With