To IO. Writer as an example to see the interface{} in Go

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

Some understanding after reading this article

1 io. Writer interface

Io. The writer interface is defined as follows

Package IO    Type Writer interface {    write (P []byte) (n int, err error)}//According to the features of the interface in go, all implemented the type of the Write method, we say it implements IO. Writer interface.

2 SOMEPKG.ABC type implements the Io.writer interface

Package Somepkgtype ABC struct {    id int}func (a *abc) Write (P []byte) (n int, err error) {    //writing ....    return 1,nil   //analog returns, n means write a few bytes, err indicates whether there is an error}//according to the features of the interface in go, because the ABC type implements the Write method, we say that the ABC type implements IO. Writer interface.

3 io. Application of writer

In general, we use the Println/printf/print method when using the FMT package. In fact, there are fprint sequence methods in the FMT package, and the Fprint sequence method is called internally by the print sequence method. Take Fprintln as an example to see the definition of the method:

Func fprintln (w io. Writer, a ... interface{}) (n int, err error)

The first parameter of a method is IO. Writer, that is to say, any implementation of IO. The type instances of the writer interface can be passed in;
Let's look at the internal implementation of the Println method:

Func Println (A ... interface{}) (n int, err error) {    return fprintln (OS). Stdout, a ...)}

We might as well retrace the OS. Stdout:

Stdout = NewFile (UIntPtr (syscall. Stdout), "/dev/stdout")

This is the standard output.
From here we can see

    1. NewFile method New out of the Os.file has the Write method, then os.file this struct has implemented the Io.writer interface
    2. The above = number illustrates the OS. StdOut is the NewFile method new, he is the OS. File
    3. Combined with the previous 1 and 2, if a method requires an IO. If the writer interface is passed in as a parameter, it can take the OS completely. StdOut as a io.writer interface to use

Look again at the definition of the Fprintln method:

Func fprintln (w io. Writer, a ... interface{}) (n int, err error)

If the first parameter is passed an OS. Stdout, the content will be output to the standard output, which is the screen.
If the first parameter passes an ordinary file, the content is output to the file.
If the first parameter is passed bytes. Buffer, then the content is output to buffer.

4 combined with the somepkg above

Package Mainimport "somepkg" import "FMT" Func Main () {    A: = &somepkg.abc{1}    i,e: =fmt. Fprintln (A, "Hello World")    FMT. Println (i)  //1}

"Hello World" will be written to a, specifically how to write, called the ABC write method
Of course, the current write method in the example ABC is not yet written, just return 1 and nil, so the next 1 will be printed out

5 Last

Interface naming is generally "action +er", understood as what can be what kind of "people", such as writer as can be understood as "will write people"
Once someone (a struct) has the ability to write (implement the Write method), we automatically think that he is a "can write", is a writer

Any struct that implements the Write method can be passed as io.writer to the related method (such as the Fprintln method)

Sorry, more verbose, in order to understand later

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.