Go in interface embedding and interface conversion

Source: Internet
Author: User

// code_22_interface_field_interface project main.gopackage mainimport ( "fmt")type Humaner interface { SayHi()}type Personer interface { Humaner Sing(lyrics string) //lyrics 歌词}type Student struct { name string score float64}func (s *Student) SayHi() { fmt.Printf("Student[%s, %f]say hi!\n", s.name, s.score)}func (s *Student) Sing(lyrics string) { fmt.Printf("Student sing[%s]!!\n", lyrics)}func main() { s := &Student{"ck_god", 88.88} var i2 Personer i2 = s //接口指向struct对象 i2.SayHi() i2.Sing("god is a girl") //var i3 Humaner = &Student{"ck_god", 88.88} //var i4 Personer = i1 //err //Personer为超集,Humaner为子集 var i5 Personer = &Student{"ck_god", 88.88} var i6 Humaner = i5 i6.SayHi()}//总结:通过赋值或者参数传递的形式,完成接口的调用。

Interface embedding and interface conversion in Go

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.