What is the difference between Type Alias and Type Definition in Go?
Asked Answered
A

4

27

Type alias:

type A = string

Type definition:

type A string

What is the difference between them? I can't understand from spec

Achromic answered 16/4, 2020 at 10:20 Comment(4)
Mean when i use type A = string, so A will exactly behave like string, and can do stuff that can do with string type without any type casting, like :- var p A = "sfsdf"; p + "sdfds" works ? and in type A string, a new type is created, ?Achromic
Both forms are type declarations, the 1st form is using "alias declaration", the 2nd form is using "type definition".Luminosity
play.golang.com/p/jDiilL424d8Luminosity
Type aliases serve no purpose during "programming", there is literally nothing what they do. They become useful during large scale refactoring of code. You'll find the lively debate online which happened prior to their introduction. You basically never need them (and you should not use them!) until you need them.Parisian
U
27

type A = string creates an alias for string. Whenever you use A in your code, it works just like string. So for example, you can't define methods on it.

type A string defines a new type, which has the same representation as string. You can convert between an A and string at zero cost (because they are the same), but you can define methods on your new type, and reflection will know about the type A.

For example (on the playground)

package main

import (
    "fmt"
)

type A = string
type B string

func main() {
    var a A = "hello"
    var b B = "hello"
    fmt.Printf("a is %T\nb is %T\n", a, b)
}

Output:

a is string
b is main.B
Understanding answered 16/4, 2020 at 10:33 Comment(6)
Alias only for compile time ? @paul-hankinAchromic
I don't believe there's any representation of the alias in the runtime reflection data, so it can be thought of as a compile-time alias. Does that answer your question?Understanding
@PaulHankin Actually, you can define methods on type alias, You just can't do that in this particular case. Please, see my answerSulphonamide
@PaulHankin Ah, sorry, misread it. like string. So for example, you can't define methods on it -- i would suggest to rephrase that for clarity.Sulphonamide
@GrigoriyMikhalkin Or you could say that you can't define methods on the type alias, but you can sometimes define methods on the original type using the alias. I think what I wrote is accurate (and helpful in the context of the question), but I agree that it doesn't cover all the edge-cases.Understanding
@PaulHankin is my understanding correct: type definition is like forking. The new type has the same memory layout (fields) as the forked type, which allows one to be converted to the other. However, the new behavior (methods) we define on the forked type won't be available on the fork, and vice versaMaddock
S
10

To be clear on terms, these both are type declarations.

type A = string

This is called alias declaration. I.e. you creating an alias to a type. Basically, there's no difference between type and alias. You can define methods on alias and they will be available for initial type instances. Example:

type A struct {}

type B = A

func (B) print() {
    fmt.Println("B")
}

func main() {
    a := A{}
    b := B{}

    a.print()  // OUTPUT: B
    b.print()  // OUTPUT: B
}

Although, in your particular example type A = string you can't define methods on it, because string is non-local type(there was a proposal to add ability to create methods for non-local types but it got rejected).

Your second case type A string is type definition. Basically it's creating new type that has all fields of original type, but not it's methods. Example:

type A struct {}

func (A) print() {
    fmt.Println("A")
}

type B A

func (B) print() {
    fmt.Println("B")
}

func main() {
    a := A{}
    b := B{}

    a.print()  // OUTPUT: A
    b.print()  // OUTPUT: B
}
Sulphonamide answered 16/4, 2020 at 11:1 Comment(0)
A
3

The form type A = string creates an alias between the two, they cannot be extended at that stage. What purpose does that solve? Well, for golang they help for certain things like codebase refactoring & repair and there's a great article about it here: https://talks.golang.org/2016/refactor.article.

Type definition allows you to extend a type with some additional behaviour if you require it, this will generally be the one that is most useful as a developer when you want to build custom datatypes from basic datatypes.

Along answered 16/4, 2020 at 10:37 Comment(0)
U
0

Type Alias

type A = string

This is alias declaration. Alias, by definition, is giving another name. And so, in golang, it this used to give the type (here, 'string') another name (here, 'A'). Then you can define new variables with new alias to that type.
Eg:

var myString A

Type Definition

type A string

This is defining a type.
It is very much similar to defining a variable.

var randomNumber int

Here, 'A' is a new type. But you can manipulate the newly defined type A by adding new methods to it.

Undenominational answered 13/7, 2022 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.