Convert byte slice to io.Reader in Golang [SOLVED]


GO

Author: Tuan Nguyen
Reviewer: Deepak Prasad

In this post, we'll examine how to translate a byte slice into an io.Reader in Golang. In Go, a byte slice is a dynamic array (slice) of bytes, which can represent any type of data in memory, including file contents, data sent over the network, etc. It might be advantageous to convert a byte slice into an io.Reader because it has the Read method so it enables us to combine it with other elements of our application and the Go standard library.

 

Example 1: Convert a byte slice to io.Reader

Let's look at an example of how to convert a byte slice to an io. Read in Golang. We can use the NewReader() function to convert a byte slice to bytes.Reader which implements the io.Reader interface. To learn a little bit more about interfaces in Go, read Effective Go: Interfaces and Types.

func NewReader(b []byte) *Reader: NewReader returns a new Reader reading from b.

package main

import (
	"bytes"
	"fmt"
	"log"
)

func main() {
	testData := []byte("this is some random data in GoLinuxCloud example!")

	// byte slice to bytes.Reader, which implements the io.Reader interface
	reader := bytes.NewReader(testData)

	// read only 10 byte from our io.Reader
	buf := make([]byte, 10)

	if _, err := reader.Read(buf); err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(buf))
}

Output:

this is so

 

Example 2: Convert an io.Reader to a byte slice

Let’s see an example of how we can convert an io.Reader to a byte slice in Go using the Buffer type:

type Buffer: A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.

package main

import (
	"bytes"
	"io"
	"log"
	"strings"
)

func main() {
	reader := strings.NewReader("this is some random data in GoLinuxCloud example!")

	// a bytes.Buffer and read from io.Reader
	buf := new(bytes.Buffer)
	buf.ReadFrom(reader)

	// get a byte slice from bytes.Buffer
	data := buf.Bytes()

	// only read the first 10 bytes
	log.Println(string(data[:10]))
}

Output:

2022/12/22 20:13:06 this is so

 

Example 3: Convert an io.Reader to a string

Method-1: Using bytes.Buffer

We also can convert an io.Reader() to a string as the code shown below:

package main

import (
	"bytes"
	"io"
	"log"
	"strings"
)

func main() {
	reader := strings.NewReader("this is some random data in GoLinuxCloud example!")

	// a bytes.Buffer and read from io.Reader
	buf := new(bytes.Buffer)
	buf.ReadFrom(reader)

	// return a string
	data := buf.String()

	// print the string
	log.Println(data)
}

Output:

2022/12/22 20:18:26 this is some random data in GoLinuxCloud example!

 

Method-2: Using strings.Builder

Another way to convert to string is using the strings.Builder type:

type Builder: A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.

package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	reader := strings.NewReader("this is some random data in GoLinuxCloud example!")

	// byte slice to bytes.Reader, which implements the io.Reader interface
	buf := new(strings.Builder)
	_, err := io.Copy(buf, reader)
	if err != nil {
		fmt.Println(err)
	}
	// check errors
	fmt.Println(buf.String())
}

Output:

this is some random data in GoLinuxCloud example!

 

Summary

In this tutorial, we can come to the conclusion that: To get a type that implements io.Reader from a []byte slice, we can use bytes.NewReader in the bytes package. We can use the Buffer type to convert from an io.Reader() to a byte slice or a string.

 

References

https://pkg.go.dev/bytes#NewReader
Convert byte slice to io.Reader - Stack Overflow
Is there a way in go to convert a []byte slice to an io.Reader?

 

Tuan Nguyen

Tuan Nguyen

He is proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment