当前位置:网站首页>How to convert []byte to io. in go Reader?
How to convert []byte to io. in go Reader?
2022-06-23 20:45:00 【AlwaysBeta】
Link to the original text : How to be in Go Lieutenant general []byte Convert to io.Reader?
stay stackoverflow I saw a problem on , The subject made a network request , The interface returns []byte. If you want to convert it to io.Reader, What needs to be done ?
This problem is not complicated to solve , You can easily convert it to success with a few lines of code . More Than This , You can also reverse convert it back through a few lines of code .
Now listen to me slowly blow , First, look directly at two pieces of code .
[]byte turn io.Reader
package main
import (
"bytes"
"fmt"
"log"
)
func main() {
data := []byte("Hello AlwaysBeta")
// byte slice to bytes.Reader, which implements the io.Reader interface
reader := bytes.NewReader(data)
// read the data from reader
buf := make([]byte, len(data))
if _, err := reader.Read(buf); err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))
}Output :
Hello AlwaysBeta
This code will first []byte Data conversion to reader in , And then from reader Read data from , And print out .
io.Reader turn []byte
package main
import (
"bytes"
"fmt"
"strings"
)
func main() {
ioReaderData := strings.NewReader("Hello AlwaysBeta")
// creates a bytes.Buffer and read from io.Reader
buf := &bytes.Buffer{}
buf.ReadFrom(ioReaderData)
// retrieve a byte slice from bytes.Buffer
data := buf.Bytes()
// only read the left bytes from 6
fmt.Println(string(data[6:]))
}Output :
AlwaysBeta
This code first creates a reader, Then read the data to buf, Last printout .
The above two codes are []byte and io.Reader The process of mutual transformation . Comparing these two pieces of code is not difficult to find , There are NewReader The figure of . And in the conversion process , Have played a key role .
So here comes the question , This NewReader What is it ? Next, let's explore the source code .
The source code parsing
Go Of io The package provides the most basic IO Interface , among io.Reader and io.Writer Two interfaces are the most critical , Many native structures revolve around these two interfaces .
Let's talk about these two interfaces respectively :
Reader Interface
io.Reader Represents a reader , It reads data from a resource to the transfer buffer . In the buffer , Data can be streamed and used .
The interface is defined as follows :
type Reader interface {
Read(p []byte) (n int, err error)
}Read() Methods will len(p) Bytes read to p in . It returns the number of bytes read n, And the error message when an error occurs .
For example :
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main() {
reader := strings.NewReader("Clear is better than clever")
p := make([]byte, 4)
for {
n, err := reader.Read(p)
if err != nil {
if err == io.EOF {
fmt.Println("EOF:", n)
break
}
fmt.Println(err)
os.Exit(1)
}
fmt.Println(n, string(p[:n]))
}
}Output :
4 Clea 4 r is 4 bet 4 ter 4 than 4 cle 3 ver EOF: 0
This code starts from reader Constantly reading data , Every time I read 4 Bytes , Then print out , Until the end .
The last time I returned n The value may be less than the buffer size .
Writer Interface
io.Writer Represents a writer , It reads data from the buffer , And write the data to the target resource .
type Writer interface {
Write(p []byte) (n int, err error)
}Write Methods will len(p) Bytes from p Write to the object data stream . It returns from p Number of bytes written in n, And the error information returned when an error occurs .
For example :
package main
import (
"bytes"
"fmt"
"os"
)
func main() {
// establish Buffer Temporary storage space , And write a string to Buffer
// Use io.Writer Of Write Method write
var buf bytes.Buffer
buf.Write([]byte("hello world , "))
// use Fprintf Splice a string into Buffer in
fmt.Fprintf(&buf, " welcome to golang !")
// take Buffer Output the content of to the standard output device
buf.WriteTo(os.Stdout)
}Output :
hello world , welcome to golang !
bytes.Buffer It's a structural type , Used to temporarily store written data , Its implementation io.Writer Interface Write Method .
WriteTo Method definition :
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
WriteTo The first parameter of the method is io.Writer Interface type .
Conversion principle
Let's go back to the conversion problem at the beginning of the article .
As long as an instance implements the interface io.Reader The method in Read() , The interface is satisfied io.Reader.
bytes and strings All packages are implemented Read() Method .
// src/bytes/reader.go
// NewReader returns a new Reader reading from b.
func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }// src/strings/reader.go
// NewReader returns a new Reader reading from s.
// It is similar to bytes.NewBufferString but more efficient and read-only.
func NewReader(s string) *Reader { return &Reader{s, 0, -1} } Calling NewReader When , Will return the corresponding T.Reader type , And they all pass io.Reader Extended from , So the conversion is realized .
summary
In the development process , It is inevitable that some IO operation , Including printouts , File read and write , Network connection, etc. .
stay Go In language , A series of standard libraries are also provided to deal with these operations , It is mainly packaged in the following packages :
io: Basic IO Operation interface .io/ioutil: Encapsulates some practical IO function .fmt: Realized IO Format operation .bufio: Realized the buffered IO.net.Conn: Network reading and writing .os.Stdin,os.Stdout: System standard input and output .os.File: System file operation .bytes: Byte dependent IO operation .
except io.Reader and io.Writer outside ,io The package also encapsulates many other basic interfaces , such as ReaderAt,WriterAt,ReaderFrom and WriterTo etc. , I won't introduce them here . This part of the code is not complicated , It's easy to read , And it can also deepen the understanding of the interface , Let's see .
Okay , That's all for this article . Pay attention to me , Take you through the question to read Go Source code .
Reference article :
边栏推荐
- Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
- [open source] goravel (golang Web Framework) - new cache module
- 【Golang】快速复习指南QuickReview(八)——goroutine
- 手续费佣金低的券商,华泰证券网上开户安全吗
- Digital procurement transformation solution: SaaS procurement management platform promotes enterprise sunshine procurement
- Configure two databases in master-slave database mode (master and slave)
- Add two factor authentication, not afraid of password disclosure, let alone 123456
- How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
- WinDbg loads mex DLL analysis DMP file
- Kinsoku Jikou Desu Sina stock interface change
猜你喜欢
Implementing MySQL fuzzy search with node and express

Add two factor authentication, not afraid of password disclosure, let alone 123456

Tcp/udp Fundamentals

LeetCode 260. Number III that appears only once

Ugeek's theory 𞓜 application and design of observable hyperfusion storage system

Kubernetes resource topology aware scheduling optimization

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

SQL聯合查詢(內聯、左聯、右聯、全聯)的語法

ZABBIX monitoring - Aruba AP operation data
随机推荐
The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
【Golang】快速复习指南QuickReview(八)——goroutine
Teach you how to develop desktop applications with web pages
How to make a commodity price tag
How to build a cloud game platform on the server? How to select a cloud game server?
怎么开户?在国海证券开户安全吗?需要带什么?
Do you need a server to set up cloud on demand? What services can cloud on demand provide?
What is the role of computer auto audit audio? What content failed to pass the audit?
JS advanced programming version 4: generator learning
How do I view the server when I log in to the fortress machine? Operation guide for novice
What technology is used for video intelligent audit? Difficulties encountered in video audit?
Applet development framework recommendation
What is the role of short video AI intelligent audit? Why do I need intelligent auditing?
小程序开发框架推荐
[open source] goravel (golang Web Framework) - new cache module
【Golang】怎么实现Go程序的实时热更新
[golang] quick review guide quickreview (VI) -- struct
【Golang】快速复习指南QuickReview(一)——字符串string
国元期货交易软件正规吗?如何安全下载?