从Writer获取字符串的最佳方法

huangapple go评论87阅读模式
英文:

The best way to get a string from a Writer

问题

我有一段代码,它使用内置的模板系统返回一个网页。它接受一个ResponseWriter,将生成的标记写入其中。现在,我想将生成的标记作为字符串获取,并在某些情况下将其放入数据库中。我将一个接受普通Writer而不是ResponseWriter的方法提取出来,现在正在尝试获取已写入的内容。啊哈 - 也许我需要一个Pipe,然后我可以使用bufio库中的ReadString方法获取字符串。但事实证明,从管道中出来的PipeReader与我需要的Reader(用于ReadString方法)不兼容。哇哦,大惊喜。所以我可以使用PipeReader读取字节数组,但感觉有点不对,因为有ReadString方法存在。

那么,最好的方法是什么?我应该继续使用Pipe并读取字节,还是有更好的方法我在手册中没有找到?

英文:

I have a piece of code that returns a web page using the built-in template system. It accepts a ResponseWriter to which the resulting markup is written. I now want to get to the markup as a string and put it in a database in some cases. I factored out a method that accepts a normal Writer instead of a ResponseWriter and am now trying to get to the written content. Aha - a Pipe may be what I need and then I can get the string with ReadString from the bufio library. But it turns out that the PipeReader coming out from the pipe is not compatible with Reader (that I would need for the ReadString method). W00t. Big surprise. So I could just read into byte[]s using the PipeReader but it feels a bit wrong when ReadString is there.

So what would be the best way to do it? Should I stick with the Pipe and read bytes or is there something better that I haven't found in the manual?

答案1

得分: 187

如果你的函数接受一个io.Writer,你可以传递一个*bytes.Buffer来捕获输出。

// import "bytes"
buf := new(bytes.Buffer)
f(buf)
buf.String() // 返回写入到它的字符串

如果它需要一个http.ResponseWriter,你可以使用一个*httptest.ResponseRecorder。响应记录器保存了可以发送到ResponseWriter的所有信息,但是body只是一个*bytes.Buffer

// import "net/http/httptest"
r := httptest.NewRecorder()
f(r)
r.Body.String() // r.Body是一个*bytes.Buffer
英文:

If your function accepts an io.Writer, you can pass a *bytes.Buffer to capture the output.

// import "bytes"
buf := new(bytes.Buffer)
f(buf)
buf.String() // returns a string of what was written to it

If it requires an http.ResponseWriter, you can use a *httptest.ResponseRecorder. A response recorder holds all information that can be sent to a ResponseWriter, but the body is just a *bytes.Buffer.

// import "net/http/httptest"
r := httptest.NewRecorder()
f(r)
r.Body.String() // r.Body is a *bytes.Buffer

答案2

得分: -7

以下是翻译好的部分:

下面的代码可能是将Writer(或任何类型)转换为字符串的最简单方法

package main

import "fmt"
import "io"
import "reflect"

func main(){
    var temp io.Writer
    output := fmt.Sprint(temp)
    fmt.Println(reflect.TypeOf(output))

}

输出:

string
英文:

The below code is possibly the simplest way to convert Writer (or any type) to string

package main

import "fmt"
import "io"
import "reflect"

func main(){
    var temp io.Writer
    output := fmt.Sprint(temp)
    fmt.Println(reflect.TypeOf(output))

}

Output:

string

huangapple
  • 本文由 发表于 2012年12月7日 23:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/13765797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定