Golang type type definition and assignment

type A string
type B []string
func main() {
    one := "string"
    two := []string{"string"}
    var a A
    var b B
    b = two 
    a = one   // cannot use one (type string) as type A in assignment
}

in the code above, it"s easy to understand that a=one is incorrect, thinking that An is already a new type
so why is b=two OK? How to understand?

Jul.07,2021

this thing can be regarded as a go trap, or you can call it a feature (/ laugh)

for slice type, as long as the underlying type is the same, it is considered to be the same type. You can replace B with type B [] A and try

.
Menu