On interface in Go language

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

has been in touch with the go language for a few months, although most of the system package is not very skilled, but for go is also a certain understanding. The go language does not have a very bright spot on the language level. In terms of efficiency, the go language is less than C + +, and in terms of portability the go language is less than Java, and Erlang can do well in high concurrency. So go gives people a sense of omnipotence and makes the right choice in every way. Go will be faster in development efficiency.

In the Go language, interface is the focus. In object-oriented design, there is a combination better than the inheritance of this sentence. The go language gives up inheritance and is well-behaved in terms of composition. First, let's look at the usage of interface. There are two ways to use interface:

1, NULL interface, people who have used C + + to see the definition of an empty interface The first thing that comes to mind is the void* in C + +.

var businesser interface{}var Intdata int = 100var StringData string = "I love HxC" businesser = intdatavar IntData2 int = Businesser. (int) fmt. PRINTLN (Businesser)//100FMT. Println (IntData2)//100businesser = Stringdatavar StringData2 string = Businesser. (string) fmt. PRINTLN (STRINGDATA2)//"I lobe HxC" FMT. PRINTLN (Businesser)//"I love HxC"

From the above usage, the use of an empty interface does appear to be consistent with the void* in C + +. But a variable of the void* type that is not known from a C + + (of course, from a programming point of view, this is to be avoided), we cannot judge its original type, in the go language for an empty interface can be through type assertion, Or use the reflect package in the System package to restore the data type that was originally assigned to an empty interface.

Func interfaceusage (RHS interface{}) {switch val: = RHS. ( Type) {case int: ... case string: ...}}

From the description of the null interface above, it is known that the null interface is somewhat similar to the root object in object-oriented languages. However, there is a problem to be aware of when using interface in the Go language.

var businesser interface{}var Intdata int = 100var StringData string = "I love HxC" businesser = intdatavar IntData2 int = Businesser. (int) Intdata = 101fmt. PRINTLN (Businesser)//100FMT. Println (IntData2)//100businesser = Stringdatastringdata = "HxC Love Me Too" var StringData2 string = Businesser. (string) fmt. PRINTLN (STRINGDATA2)//"I lobe HxC" FMT. PRINTLN (Businesser)//"I love HxC"

From the output of the above code, you can see that when a variable is assigned to an interface, it copies a copy of the variable to an empty interface. This causes the contents of the variables we see to change, and the contents of the variables saved by the Null interface are unchanged. The way to avoid this problem is to assign the address of the variable to an empty interface, which guarantees both content consistency and avoids a lot of duplication. This aspect of the problem is consistent in the non-null interface.

2, non-null interface


Inheritance is not explicitly supported in the

    go language. But it also provides a way to implement inheritance, and is not very intuitive to implement. It is recommended to use a non-null interface in the go language. The non-null interface uses the following:

Type personer interface {    walk ()     changename ( String)}type goodperson struct {    name string}func  Newgoodperson (name string)  *goodperson {    return &goodperson{ name}}func  (This *goodperson) Walk ()  {    fmt. Println (this.name,  " walking")}func  (This *goodperson) changename (name string)  {     this.name = name}func  (This *goodperson) Fly ()  {     fmt. Println ("Man can ' T fly")}func main ()  {    var Person  Personer = newgoodperson ("HxC")     var person2 interface{} =  person        person.walk ()  //  "hxc walking"     person.changename ("Wzm")   &nbsP; person.walk ()  //  "wzm walking"     // person.fly ()  //  error Personer have no field or mothed name Fly    // person2.walk ()  // error   //person = 1 //error}

A data type can assign a value to a non-null interface if the method of the interface is implemented. If the data type does not implement a method in a non-null interface, it cannot be assigned to a non-null interface. A nullable interface is an assignment interface, and its underlying copy, so the way data types implement interfaces is better than passing pointers. Because the non-null interface is called, the content assigned to the non-null interface variable can be changed.

In the go language, a data type does not have to explicitly describe which interface is implemented, as in other languages. Therefore, a data type can implement a variety of interfaces. Since there is no constructor in the go language like C + +, it is customary for the go language to produce specific data variables through a factory.

Related Article

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.