This is a creation in Article, where the information may have evolved or changed. [Cover] (https://raw.githubusercontent.com/studygolang/gctt-images/master/identical-type/cover.jpg) [Assignable in the Go language (assignability in Go)] The first example of (https://studygolang.com/articles/12381) refers to the:> if the right-hand type and T are equivalent, then the assignment is completely (done). This may sound trivial, but there are some details (worth noting). Delving into this topic may also help to understand other relevant basic concepts of language. # # Type declaration has a special statement to create a new type, which is to use the Type keyword: ' ' gotype A struct{name string}type B atype (C stringd map[string]int) ' # # Basic class Each type in the go language has a lower-level type. Ordinary blocks contain some pre-declared identifiers bound to the type Boolean, string, or numeric, and so on. For each pre-declared type T, its underlying type is T (there is no trap here). Their type literals are the same: ' ' ' go//Sample type Literalsvar (a [10]INTB struct{name string}c *intd func (p int) (r int) e interface {f (in t) int}f []INTG map[string]inth chan<-string) ' > type declaration can be "factored" into blocks to avoid repeating the keyword var multiple times. The same method can be used for type declarations, just like the first piece of code. In other cases, the underlying type of T is bound to the type's underlying type through the type declaration: ' ' Gotype x string//underlying type of x is StringType y x//underlying type of y is St Ringtype Z [10]int///underlying type of z is [10]int ' # # Name/nameless type named type is a new type specified by the type name and can be prefixed with the package name. The package name is used to access the naming that is exported from other packages. (with the previous appropriate import statement)Gopackage mainimport "FMT" type T FMT. Formatter//T and FMT. Formatter is named types ' restrictive identifier (prefixed with the package name) cannot refer to this package: ' ' gopackage footype A struct{name string}type B foo. A//compiler throws "Undefined:foo in Foo. A "'" Unnamed type uses a type constant like F.ex to refer to itself:-map[string]int-chan<-int-[]float32## type identity type identifies the underlying concepts, and understands when the two types in the Go language The same or different is easy: 1. When created by the same type declaration, two named types are equivalent: ' ' Gotype (T1 stringT2 string) ' T1 and T1 are equivalent. Because of the use of two separate type declarations, T1 and T2 are different (even if they are factored into a block). 2. Named and unnamed types are different (no exceptions) 3. If the corresponding type literals are the same (type literals details are described in [language spec] (https://golang.org/ref/spec#Type_identity ), then the unnamed type is equivalent.
Via:https://medium.com/golangspec/identical-types-in-go-9cb89b91fe25
Author: Michałłowicki Translator: Leemeans proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
238 Reads