Type alias:
type A = string
Type definition:
type A string
What is the difference between them? I can't understand from spec
Type alias:
type A = string
Type definition:
type A string
What is the difference between them? I can't understand from spec
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
like string. So for example, you can't define methods on it
-- i would suggest to rephrase that for clarity. –
Sulphonamide 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
}
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.
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 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.
© 2022 - 2024 — McMap. All rights reserved.