How to convert a byte slice ([]byte) to io.Reader in Golang?
Hello everyone! I just started learning Go and encountered a problem when working with data streams. I have a function that accepts the `io.Reader` interface, but I'm working with data that is already in memory as a byte slice (`[]byte`). \n\nHere's a simplified example. Let's say I receive data from an HTTP request or read a file, but for some reason I already have `[]byte`:\n\n```go\npackage main\n\nimport (\n "fmt"\n "io"\n "log"\n)\n\nfunc processData(r io.Reader) {\n data, err := io.ReadAll(r)\n if err != nil {\n log.Fatal(err)\n }\n fmt.Println("Received:", string(data))\n}\n\nfunc main() {\n myBytes := []byte("Hello, World! This is test data.")\n // How to pass myBytes to processData?\n // processData(myBytes) // Compilation error!\n}\n```
No answers yet.
Log in to leave an answer