Brother Lian Blockchain training share go language Golang combination and interface

Source: Internet
Author: User

As a low-level technology, blockchain technology from the initial digital money to smart contracts to the current industry scenarios distributed application implementation, since 2008, 10 years ago, the blockchain from the original theoretical concept to today's actual scenario application landed, although questioned but have been hard to move forward. Especially in the last two years, the re-construction of the existing industry logic in blockchain has made it undisputed the next development of the wind.


Brother Lian Education go language + blockchain Training College is comprised of Yin Cheng, the world's most valuable expert in the field of Microsoft Blockchain, and Qinghua's unnamed teaching team, with a learning cycle of 5.5 months covering the go language, Blockchain cryptography, distributed programming, consensus algorithms, public-link development, dapp-tai = square and intelligent contract development, blockchain system framework development Super Ledger and Blockchain 3.0EOS and other content.


One, combination

Everything is a type in Golang


It's a bit like the object-oriented concept, but it's not quite like it, and there's something in common with JavaScript.


So how to implement inheritance like in Java, Golang uses a combination


Please look at the code and run the output to explain everything


Type Father struct {

Name string

Sex int

}

Type Sun struct {

Father

Name string

}

Func Main () {

S:=sun{father:father{name: "Father", Sex:11},name: "Sun"}

Fmt. PRINTLN (s)

Fmt. Println ("name", S.name)

Fmt. Println ("name", S.father.name)

Fmt. Println ("Sex", s.sex)

Fmt. Println ("Sex", s.father.sex)

}


=======================================


{{Father} Sun}

Name Sun


Name Father

Sex 11

Sex 11

===================================


Father's sex attributes were combined into Sun's properties, although my name was father and Sun but the two were not inherited as a combination of relationships


When the Father property has the same name as Sun's property, the group is the parent Sun priority, and the need to access father's name requires S.father.name


Two. Interface

Interfaces can be combined in a way that is equivalent to adding other interfaces to the interface.

Type Reader Interface {

Read ()

}

Type Writer Interface {

Write ()

}


The implementation class that defines the two interfaces above

Type Myreadwrite struct{}


Func (MRW *myreadwrite) read () {

Fmt. Println ("Myreadwrite...read")

}


Func (MRW *myreadwrite) write () {

Fmt. Println ("Myreadwrite...write")

}


Defines an interface that combines the two interfaces above

Type Readwriter Interface {

Reader

Writer

}


The above interface is equivalent to:

Type ReadWriterV2 Interface {

Read ()

Write ()

}


The Readwriter and ReadWriterV2 two interfaces are equivalent, so you can assign values to each other

Func interfaceTest0104 () {

MRW: = &myreadwrite{}

The MRW object implements the Read () method and the Write () method, so you can assign values to Readwriter and ReadWriterV2

var rw1 readwriter = MRW

Rw1.read ()

Rw1.write ()


Fmt. PRINTLN ("------")

var rw2 ReadWriterV2 = MRW

Rw2.read ()

Rw2.write ()


At the same time, Readwriter and ReadWriterV2 two interface objects can be assigned to each other

RW1 = RW2

RW2 = Rw1

}


Three. Interfaces and combinations

1. Because struct struct implements all the methods of the interface, the struct can be assigned to the corresponding interface

2. Implementation of all the methods of the interface has been implemented in STRUCT2

3. And struct1  if the combination of Struct2, then Struct1 also implements all the methods of the interface implementation, so you can assign Struct1 to the interface

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.