Golang Interface Parameters

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
conn, err = ln. Accept () go handleconnection (conn)

See here I have a question, why not handleconnection (&conn)?

Package Mainimport ("FMT") type Interface Interface {say () String}type Object struct {}func (this *object) say () St Ring {return "Hello"}func do (i Interface) string {return I.say ()}func main () {o: = object{} fmt. Println (Do (&o)) fmt. Printf ("Ccccccccccc:%t", O)}

The parameters of the function are defined by the interface, and the compiler determines itself whether the argument is an object or a pointer to the object
For example, say is a method on a pointer, so do only accept pointers to object as arguments, do (o) is compiled

So when you see the library interface as a parameter type definition, you can simply think that this interface is definitely an object pointer (although you can also use the object, single estimate no class library will be used)

For example:

conn, err = ln. Accept () go handleconnection (conn)

Here conn is an interface, do not need go handleconnection (&conn)


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.