[Go]将string转换为io.Reader类型

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:25   1421   0

在使用很多函数的时候需要传入string字符串 , 但是函数参数类型是io.Reader , 这时候就需要将string转换为Reader类型

例如下面的:

strings.NewReader("aaaa")

NewReader返回从读取的新Reader。 它类似于bytes.NewBufferString,但效率更高且只读。

    bytes.NewBuffer([]byte("aaaaa"))
    bytes.NewBufferString("aaaa")
    bytes.NewReader([]byte("aaaa"))

Buffer是具有Read和Write方法的可变大小的字节缓冲区。 Buffer的零值是可以使用的空缓冲区。

上面这俩都行

strings.NewReader(string(body))
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP