Understanding the Go Type System

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Classification of type

All objects that need to be defined and declared in Go are type,int,string,function,pointer,interface,map,struct and so on.

Like most computer languages, Go type defaults to the common underlying data type, boolean,numeric and string, which are called pre-declarered types, These basic data can further form more complex type Array,struct,map,slice,channel and so on.

The type can be a name and no name, called named type and unnamed type.

Named Types is an individual name for an existing type by using the Type keyword, such as type NewType ExistingType NewType is the name.

pre-declared Types is also named Types.

unamed Types is a literal type, which is no name, only the type itself, like this [6]int without a name.

Each type has its own underlying type , and if T is pre-declared type or type literal, their corresponding underlying type is self t, otherwise t's Underlyi NG type is the underlying type that is referenced when the T definition is defined.

Underlying type

If two type are named type, they cannot be assigned to each other

123
type string var string ="a"var//cannot Use my (type string) as type newstring in assignment

Although their underlying type is string, the string type of my cannot be assigned to the newstring type of you.

If one of the two type is unamed type, you can assign values to each other

1234567891011121314151617
 PackageMaintypePTR *inttypeMapMap[int]stringtypeMapmap Map func main() {varP *int    varMM MapvarMmm MapmapvarM1Map[int]string= mmvarM2Map[int]string= MMMvarptr ptr = PPrint(PTR)Print(M1)Print(m2)}

Why is there such a distinction?

If you name a type, you want to make a distinction, so two named types cannot be assigned to each other even if underlying name is the same.

See Google Group Topic

Named type and unamed type

When named types is used as a function receiver, it has its own method, unamed types is not, this is their important difference.

1234567891011121314151617
 package  mainimport  () type  newmap map  [int ]string  func   (nm newmap)  add   (key int , value string )   {Nm[key] = value} func  main   ()   {var  p newmap = make  ( map  [int ]    string ) P.add (10 , ) fmt. PRINTLN (p) //map[10:a] } 

One of the exceptions is that pre-declare types can't have his own way.

1234567891011
 Package mainfuncint)name(){       print (n)}  funcmain()  {    varint    n.name ()}

The compiler throws cannot define new methods on non-local type int error, cannot define method for type outside of package, fix this problem is to redefine alias for pre-declared types 。

Property inheritance of type one: direct inheritance

The

Named type does not inherit the method from its underlying type or reference type, but it inherits field

 123456789101112131415161718192021222324 
 package  mainimport  ( "FMT" ) type  person struct  {name string }func   (P *person)   Speak    {fmt. Println ( "I am a Person" )}type  Student personfunc  main   ()   {var  P person p.speak () var  s Student s.nam E =  "Jone"  fmt. Println (s.name) //s.speak () } 

Named type Student does not inherit methods from person Speak, open comment execution error s.speak undefined (type Student has no field or method Speak) , but the filed name of person can be inherited by Student.

Property inheritance of type two: type embedding

If a type T ' is embedded in another type T as its filed,t ' all field and method can be used in T, this method is called type embedding.

1234567891011121314151617181920212223242526272829303132
Package Mainimport (    "FMT") type I interface {talk    ()}type person struct {    name String}func (P *person) Speak () {    FMT. Println ("I am a Person")}func (P *person) talk () {    fmt. Println ("I am Talking")}type people struct {    Person}func main () {    var people people    People.name = "People" C11/>people. Speak ()    people. Talk ()}

Type conversion

The Type can be converted to each other, but to follow certain conversion rules, see the official specification Https://golang.org/ref/spec#Conversions for details.

Reference Code

Some of the code in this article

Package Mainimport ("FMT") type I interface {talk ()}type person struct {name String}func (P *person) Speak () {fmt. Println ("I am a Person")}func (P *person) talk () {fmt. Println ("I am Talking")}type Student persontype highstudent studenttype people struct {person}func main () {var p personp.s Peak () P.talk () var s students.name = "Jone" FMT. Println (S.name) var hs highstudenths.name = "High Jone" FMT. Println (Hs.name) var i Ii = &pi. Talk () var people peoplepeople.name = "people" people. Speak () people. Talk ()//S.speak ()}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.