Golang:结构嵌入与“混叠”

I know about struct embedding

type newType struct {someStruct}

I know about type aliasing:

go type newType = someStruct

But is there any practical reason to use

type newType someStruct

What about primitive types?

type newType int

What is the right name for such a definition?

Naming: All of the snippets are type declarations. One of the declarations is a type alias (the one with the =). The remaining declarations are type definitions. The first of those definitions uses a struct with an embedded field.

The code type newType someStruct is useful when one wants to define a new type with the same memory layout as some other struct type. This might because the programmer wants to use different methods on the same memory layout.

The code type newType int is useful for defining a type with a semantic difference from int or for attaching methods to the primitive type. See reflect.Kind for one example.

struct embedding vs “aliasing”


You are conflating to different constructs.

For the definition of struct embedding, see The Go Programming Language Specification.


Here's an explanation of and the rationale for Go type aliases.

Go 1.9 Release Notes (released 2017/08/24)

Changes to the language

Go now supports type aliases to support gradual code repair while moving a type between packages. The type alias design document and an article on refactoring cover the problem in detail. In short, a type alias declaration has the form:

type T1 = T2

This declaration introduces an alias name T1—an alternate spelling—for the type denoted by T2; that is, both T1 and T2 denote the same type.