Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type alias vs type definition in Go

Tags:

go

type-alias

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
}
like image 791
ᅙᄉᅙ Avatar asked Jan 28 '19 13:01

ᅙᄉᅙ


People also ask

What is type alias in Golang?

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.

What is type alias?

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.

How do you define a type in Go?

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.

What is underlying type in Golang?

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.


3 Answers

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.

like image 180
rob74 Avatar answered Nov 15 '22 10:11

rob74


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

like image 24
Andrew W. Phillips Avatar answered Nov 15 '22 08:11

Andrew W. Phillips


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<<

like image 33
Meuko Avatar answered Nov 15 '22 10:11

Meuko