Avatar ·

How to convert a byte slice ([]byte) to io.Reader in Go?

Hello everyone! I'm new to Go and encountered a problem when working with data streams. I have a function that accepts io.Reader for reading data, for example, from a file or HTTP response. But in my case, the data is already in memory as a byte slice ([]byte), obtained after unpacking an archive or processing JSON.

\n\n

Here's a simplified example of what I'm trying to do:

\n\n
package main\n\nimport (\n    "fmt"\n    "io"\n    "os"\n)\n\nfunc main() {\n    data := []byte("Hello, World! This is test data in memory")\n\n    // How to pass data to a function expecting io.Reader?\n    // I tried just passing data, but I get a compilation error:\n    // cannot use data (type []byte) as type io.Reader in argument to processData\n    processData(d