The first experience of the interface in Go

Source: Internet
Author: User

Code_21_struct_interface_firsttime Project Main.gopackage mainimport ("FMT")/* 1) interface interface is a custom type, The interface type specifically describes a collection of methods. 2) An interface type is an abstract type that does not expose the structure of the internal values of the objects it represents and the combination of the underlying operations that the object supports, and they will only show their own methods. Therefore the interface type cannot instantiate it. 3) go through the interface to achieve the duck type (duck-typing) */type Humaner Interface {sayhi ()//1) generally end with the ER//2) interface only method declaration, no implementation, no data field//3) interface can be anonymously embedded in other interfaces, or embedded in the struct}type Student struct {name string score Float64}//student implements the Sayhi () method func (S *student) Sayhi () {fmt. Printf ("student[%s,%f") say hi!!! \ n ", S.name, S.score)}type Teacher struct {name string group String}func (t *teacher) Sayhi () {fmt. Printf ("teacher[%s,%s") say hi!!! \ n ", T.name, T.group)}type mystr stringfunc (str mystr) Sayhi () {fmt. Printf ("mystr[%s] say hi!", str)}func whosayhi (i humaner) {i.sayhi ()}func main () {//Interface implementation: 1) The interface is the type used to define the behavior. 2) These defined behaviors are not implemented directly by the interface, but are implemented by means of a user-defined type. 3) A specific type that implements these methods is an instance of this interface type. 4) If a user-defined type implements a set of methods for an interface type declaration, the value of the user-defined type can be assigned to the value of the interface type. This assignment will save the value of the user-defined type to the value of the interface type. S: = &student{"Ck_god", 88.88} t: = &teacher{"God_girl", "ComputEr_programmer "} var tmp mystr =" Character Object "S.sayhi () T.sayhi () tmp. Sayhi () fmt. Println ("\n==============\n")//polymorphic--Duck model, invoke the same interface, different manifestations whosayhi (s) whosayhi (t) whosayhi (TMP) FMT. Println ("\n==============\n") x: = Make ([]humaner, 3) x[0], x[1], x[2] = s, T, tmp for _, Value: = Range x {value. Sayhi ()} FMT. Println ("\n==============\n")}

Operation Result:

Student[ck_god,88.880000] say hi!!!Teacher[god_girl,computer_programmer] say hi!!!MyStr[字符对象] say hi!==============Student[ck_god,88.880000] say hi!!!Teacher[god_girl,computer_programmer] say hi!!!MyStr[字符对象] say hi!==============Student[ck_god,88.880000] say hi!!!Teacher[god_girl,computer_programmer] say hi!!!MyStr[字符对象] say hi!==============

The first experience of the interface in Go

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.