Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Writer and Reader in Go language

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to use Writer and Reader in the Go language. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Input and output

The design of Go Writer and Reader interfaces follows the input and output of Unix, and the output of one program can be the input of another program. Their function is single and pure, so it is very easy to write program code, and through the concept of combination, we can make our program do more things.

Var (Stdin = NewFile (uintptr (syscall.Stdin), "/ dev/stdin") Stdout = NewFile (uintptr (syscall.Stdout), "/ dev/stdout") Stderr = NewFile (uintptr (syscall.Stderr), "/ dev/stderr")

)

The input and output of these three standards are all a * File, and * File implements the two interfaces io.Writer and io.Reader at the same time, so they have the function of input and output at the same time, which can not only read data from it, but also write data to it.

The io package of the Go standard library is also based on the concept of input and output of Unix. Most of the interfaces extend io.Writer and io.Reader, and most types choose to implement the two interfaces of io.Writer and io.Reader, and then abstract the input and output of data as stream read and write. So as long as these two interfaces are implemented, the read and write function of the stream can be used.

The high degree of abstraction of the io.Writer and io.Reader interfaces allows us to focus on reading or writing instead of focusing on specific business. As long as the method function we define can accept these two interfaces as parameters, then we can read and write the stream without worrying about how to read and where to write, which is also the advantage of interface-oriented programming.

Reader and Writer interface

These two highly abstract interfaces, with only one method, also reflect the simplicity of the Go interface design, only to do one thing.

/ / Writer is the interface that wraps the basic Write method.

/ /

/ / Write writes len (p) bytes from p to the underlying data stream.

/ / It returns the number of bytes written from p (0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report