Golang The priority of anonymous field exposure methods in the GO language structure

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang The priority of anonymous field exposure methods in the GO language structure

The go language structure can contain anonymous fields, such as:

struct {    T1       //Field name automatically T1    *t2      //Field name automatically T2    p.t3     //Field name automatically T3    *p.t4    //Field name automatically T4    x, Yint  //non-anonymous field x, y}
If the struct s contains an anonymous field T, then the structure s has a method of T.

If the included anonymous field is *t, then the struct S has a *t method.
If S contains an anonymous field of T or *t, then *s has the *t method.

Like what

Type gzipresponsewriterstruct {    io. Writer}
Because IO. There is a write method in writer, so Gzipresponsewriter also has the Write method
var g gzipresponsewriterg.write (data)

The g.write here is equivalent to G.writer.write (data).

So what happens if two anonymous fields have the same method?

Type gzipresponsewriterstruct {    io. Writer    http. Responsewriter}
Io. Writer This interface already has the Write method, http. Responsewriter also has the Write method. So what is the call to G.write when it is written? Is it g.writer.write or g.responsewriter.write? You do not know the program does not know, if the compilation will appear "Write ambiguous" error.

How to solve this problem?

One: Rewrite Gzipresponsewriter's write method to indicate which side to write to.

Func (w gzipresponsewriter) Write (b []byte) (int, OS. Error) {    returnw. Writer.write (b)}
Above this is specifying the use of IO. Write method in writer.


Second: Use anonymous fields to expose method precedence to determine which method to use when repeating a method, the principle is "simple first". So here we put the HTTP. Responsewriter a little more complicated, wrap it up once with another structure.

Type responsewriter struct {    http. Responsewriter}
And then insert the structure in the Gzipresponsewriter.
Type gzipresponsewriterstruct {    io. Writer    Responsewriter}
This is IO. Writer's method is to be exposed first. This eliminates the hassle of the above, and when there are multiple repetition methods, one has to be rewritten. Remember to use Gzipresponsewriter when the original type is HTTP. Responsewriter objects such as W, to use responsewriter{w}, packaging can be used in gzipresponsewriter, such as: the creation of gzipresponsewriter structure variables, For example, using Gzipresponsewriter{w, x} on the line, now you have to use gzipresponsewriter{responsewriter{w}, X}, where w is an HTTP. The Responsewriter object.


Go and organize from: http://kejibo.com/golang-struct-anonymous-field-expose-method/

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.