How do you make a ReadCloser?

Sett 31 249 ganger
Hopp til første uleste melding

Charles

ulest,
12. juni 2010, 02:53:0512.06.2010
til golang-nuts
Nube go programmer needs help:

I want to write a go DB driver for Riak. Riak requires clients issue
HTTP GETs, PUTs, POSTs, and DELETEs to perform various DB operations.

I can't figure out how to make a "readcloser" to assign to a
http.Request.Body object. Consider the code statement below.

var req http.Request
req.Method = "PUT"
// assign more req properties here ...
req.Body = bytes.NewBufferString("foo") // won't work because a Buffer
doesn't implement read and close

req.Body = "hello" //Doesn't work because strings don't implement read
and close

How do I make a read closer? I've scoured through Go source code, but
an obvious solution hasn't jumped out at me.

Best,

Charles

John Asmuth

ulest,
12. juni 2010, 08:51:3112.06.2010
til golang-nuts
Hi,

Taking a look at io.ReadCloser, it's a union of the Reader and Closer
interfaces.

bytes.NewBufferString returns a bytes.Buffer* something that
implements Reader, so if we start with that class, we're half way
there.

You can add functionality to this class very easily.

type ClosingBuffer struct {
*bytes.Buffer
}
func (cb *ClosingBuffer) Close() (err os.Error) {
//we don't actually have to do anything here, since the buffer is
just some data in memory
//and the error is initialized to no-error
return
}

And use it like the following:

func main () {
cb := &ClosingBuffer{bytes.NewBufferString("Hi!")}
var rc io.ReadCloser
rc = cb
rc.Close()
}

- John

Michael Hoisie

ulest,
12. juni 2010, 11:58:4012.06.2010
til golang-nuts
This is how I do it for my projects:

type nopCloser struct {
io.Reader
}

func (nopCloser) Close() os.Error { return nil }

And then in your program:
req.Body = nopCloser{bytes.NewBufferString(conn.postData)}

- Mike

Charles

ulest,
12. juni 2010, 23:55:0012.06.2010
til golang-nuts
Thanks for explaining this. Compilation successful!

I had spent two nights reading documentation and writing code to no
avail. The big idea is that you can make a struct that IS a
bytes.Buffer, rather than a struct of which one of its properties is a
bytes.Buffer.

Awesome!

-C

John Asmuth

ulest,
13. juni 2010, 11:22:0713.06.2010
til golang-nuts
This way is slicker, yeah.

payton...@gmail.com

ulest,
28. aug. 2015, 20:07:2028.08.2015
til golang-nuts, charleswt...@gmail.com

borjabe...@gmail.com

ulest,
1. des. 2015, 08:29:2701.12.2015
til golang-nuts, charleswt...@gmail.com
Hi all, first, sorry for the post necromancy... but I found this question while looking and I think this might be useful for someone like me :)

How about this?

req.Body = ioutil.NopCloser(bytes.NewReader([]byte("foo")))

Best regards,

mark...@gmail.com

ulest,
7. des. 2015, 11:02:5007.12.2015
til golang-nuts, charleswt...@gmail.com, borjabe...@gmail.com
Thanks for this :-)

liuda...@gmail.com

ulest,
10. mars 2016, 02:33:2310.03.2016
til golang-nuts, charleswt...@gmail.com
Awsome~

keens...@gmail.com

ulest,
27. juni 2018, 12:51:5227.06.2018
til golang-nuts
This worked perfectly, thanks!

Ansuraj Khadanga

ulest,
1. juli 2022, 01:09:1501.07.2022
til golang-nuts
Thats great! Thanks for sharing.

req.Body = ioutil.NopCloser(bytes.NewReader([]byte("foo")))

works!

Alex Shi

ulest,
3. juli 2022, 21:47:2103.07.2022
til golang-nuts
Note. For ioutil#NopCloser, as of Go 1.16, this function simply calls io.NopCloser.
Svar alle
Svar til forfatter
Videresend
0 nye meldinger