[Go] Wrap a string in an io.ReadCloser

Created on November 28, 2026

You can use strings.NewReader and io.NopCloser together to wrap a string in a value that implements io.ReadCloser.

package main

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

func main() {
    str := "hello"
    strWithReadCloser := io.NopCloser(strings.NewReader(str))
    if _, ok := strWithReadCloser.(io.ReadCloser); ok {
        fmt.Println("Implements the io.ReadCloser interface")
    }
}

The program produces the following output.

Implements the io.ReadCloser interface
Go