Implementation of the Golang interface and considerations.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT") type Animal interface {move ()}type Human struct {i int}func (R Human) Move () {//Note FMT. PRINTLN ("Human Walk") R.i++}type Bird struct {i int}func (R *bird) move () {fmt. Println ("Bird Walk") R.i++}func moveTest1 (animal animal) {animal.move ()}//Although the definition of this function is not wrong, it is actually completely useless! Because a pointer to an interface is in the interface implementation class, it is not possible to convert the Func moveTest2 (animal *animal) {(*animal). Move ()}func main () {h1: = Human{0}movetest1 (H1) MoveTest1 (H1) MoveTest1 (H1) fmt. Println (h1.i) fmt. Println ("----------------") H2: = &human{0}movetest1 (h2) MoveTest1 (H2) MoveTest1 (H2) fmt. Println (h2.i) fmt. Println ("----------------")//h3: = Human{0}//movetest2 (h3)//movetest2 (H3)//movetest2 (H3)//fmt. Println (h3.i)////fmt. Println ("----------------")//b1: = Bird{0}//movetest1 (B1)//movetest1 (B1)//movetest1 (B1)//fmt. Println (b1.i)////fmt. Println ("----------------") B2: = &bird{0}movetest1 (b2) moveTest1 (B2) moveTest1 (B2) fmt. Println (b2.i) fmt. Println ("----------------")//b3: = &bird{0}//movetest2 (b3)//movetest2 (b3)//movetest2 (b3)//fmt. Println (b3.i)////fmt. Println ("----------------")}



Human walking
Human walking
Human walking
0
----------------
Human walking
Human walking
Human walking
0
----------------
Birds walking
Birds walking
Birds walking
3
----------------


The above uses a simple explanation of the Golang in the use of the place to pay attention to.

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.