FAQ
I'm attempting to read a remote file and then serve it back out again. I
have the following code:

var content io.ReadWriteSeeker
url := "http://localhost:8078/static/img/logo.png"
res, err := http.Get(url)
if err != nil {
return
}
var b bytes.Buffer
rb, _ := ioutil.ReadAll(res.Body)
b.Write(rb)
b.WriteTo(content)


This compiles, but barfs when I run it. (runtime error: invalid memory
address or nil pointer dereference at the "WriteTo" line.) I have the
feeling I'm doing something really dumb. Any idea what?

--

Search Discussions

  • Rémy Oudompheng at Nov 30, 2012 at 9:27 pm

    On 2012/11/30 Jaybill McCarthy wrote:
    I'm attempting to read a remote file and then serve it back out again. I
    have the following code:

    var content io.ReadWriteSeeker
    url := "http://localhost:8078/static/img/logo.png"
    res, err := http.Get(url)
    if err != nil {
    return
    }
    var b bytes.Buffer
    rb, _ := ioutil.ReadAll(res.Body)
    b.Write(rb)
    b.WriteTo(content)


    This compiles, but barfs when I run it. (runtime error: invalid memory
    address or nil pointer dereference at the "WriteTo" line.) I have the
    feeling I'm doing something really dumb. Any idea what?
    It may be because content is nil. It seems like you'd rather have a
    io.Writer w (e.g. a http.ResponseWriter), and just want to call
    io.Copy(w, res.Body)

    Rémy.

    --
  • Rodrigo Kochenburger at Nov 30, 2012 at 9:32 pm
    You're trying to right to a interface that does not point to a real
    implementation. You need to allocate an object that implements the
    ReadWriteSeeker interface and assign to the content variable.

    Also, from that code, doesn't seem you need a ReadWriteSeeker just a
    io.Writer or io.ReadWriter if you'll need to read the content on *your*
    code. For example: http://play.golang.org/p/ozWPGIPTSk


    On Friday, November 30, 2012 1:21:11 PM UTC-8, Jaybill McCarthy wrote:

    I'm attempting to read a remote file and then serve it back out again. I
    have the following code:

    var content io.ReadWriteSeeker
    url := "http://localhost:8078/static/img/logo.png"
    res, err := http.Get(url)
    if err != nil {
    return
    }
    var b bytes.Buffer
    rb, _ := ioutil.ReadAll(res.Body)
    b.Write(rb)
    b.WriteTo(content)


    This compiles, but barfs when I run it. (runtime error: invalid memory
    address or nil pointer dereference at the "WriteTo" line.) I have the
    feeling I'm doing something really dumb. Any idea what?
    --
  • Jaybill McCarthy at Nov 30, 2012 at 9:42 pm
    Okay, I see what you're saying, but I'm unsure of how to implement it in my
    case. The reason I was trying to use ReadWriteSeeker is because "content"
    is then getting passed into http.ServeContent(), which is looking for an
    io.ReadSeeker
    On Friday, November 30, 2012 1:32:43 PM UTC-8, Rodrigo Kochenburger wrote:

    You're trying to right to a interface that does not point to a real
    implementation. You need to allocate an object that implements the
    ReadWriteSeeker interface and assign to the content variable.

    Also, from that code, doesn't seem you need a ReadWriteSeeker just a
    io.Writer or io.ReadWriter if you'll need to read the content on *your*
    code. For example: http://play.golang.org/p/ozWPGIPTSk


    On Friday, November 30, 2012 1:21:11 PM UTC-8, Jaybill McCarthy wrote:

    I'm attempting to read a remote file and then serve it back out again. I
    have the following code:

    var content io.ReadWriteSeeker
    url := "http://localhost:8078/static/img/logo.png"
    res, err := http.Get(url)
    if err != nil {
    return
    }
    var b bytes.Buffer
    rb, _ := ioutil.ReadAll(res.Body)
    b.Write(rb)
    b.WriteTo(content)


    This compiles, but barfs when I run it. (runtime error: invalid memory
    address or nil pointer dereference at the "WriteTo" line.) I have the
    feeling I'm doing something really dumb. Any idea what?
    --
  • Jaybill McCarthy at Nov 30, 2012 at 10:59 pm
    Here's how I ended up solving this:

    url := "http://localhost:8078/static/img/logo.png"

    res, err := http.Get(url)
    if err != nil {
    log.Print(err)
    return
    }

    rb, err := ioutil.ReadAll(res.Body)
    if err != nil {
    log.Print(err)
    return
    }
    content := bytes.NewReader(rb)
    On Friday, November 30, 2012 1:21:11 PM UTC-8, Jaybill McCarthy wrote:

    I'm attempting to read a remote file and then serve it back out again. I
    have the following code:

    var content io.ReadWriteSeeker
    url := "http://localhost:8078/static/img/logo.png"
    res, err := http.Get(url)
    if err != nil {
    return
    }
    var b bytes.Buffer
    rb, _ := ioutil.ReadAll(res.Body)
    b.Write(rb)
    b.WriteTo(content)


    This compiles, but barfs when I run it. (runtime error: invalid memory
    address or nil pointer dereference at the "WriteTo" line.) I have the
    feeling I'm doing something really dumb. Any idea what?
    --
  • Brad Fitzpatrick at Dec 1, 2012 at 5:49 pm

    On Fri, Nov 30, 2012 at 2:59 PM, Jaybill McCarthy wrote:

    Here's how I ended up solving this:

    url := "http://localhost:8078/static/img/logo.png"

    res, err := http.Get(url)
    if err != nil {
    log.Print(err)
    return
    }

    rb, err := ioutil.ReadAll(res.Body)
    if err != nil {
    log.Print(err)
    return
    }
    content := bytes.NewReader(rb)
    What are you trying to do?

    res.Body is already an io.Reader.

    Using ioutil.ReadAll, you're slurping it all into memory rather than
    streaming it. That's okay if it's small, but what if it's big?

    I guess you want a Seeker? For http.ServeContent?

    Maybe you actually want
    http://golang.org/pkg/net/http/httputil/#ReverseProxy ?

    --
  • Jaybill McCarthy at Dec 13, 2012 at 2:48 pm
    This is for a weird edge case, wherein I need to transfer a small image
    file from Rackspace Files and then serve it up via http.ServeContent() all
    in the same pass.

    That said, if I was doing this with larger files, I like your idea of using
    httputil.ReverseProxy().

    Thanks!
    On Saturday, December 1, 2012 9:49:32 AM UTC-8, bradfitz wrote:



    On Fri, Nov 30, 2012 at 2:59 PM, Jaybill McCarthy <hey...@jaybill.com<javascript:>
    wrote:
    Here's how I ended up solving this:

    url := "http://localhost:8078/static/img/logo.png"

    res, err := http.Get(url)
    if err != nil {
    log.Print(err)
    return
    }

    rb, err := ioutil.ReadAll(res.Body)
    if err != nil {
    log.Print(err)
    return
    }
    content := bytes.NewReader(rb)
    What are you trying to do?

    res.Body is already an io.Reader.

    Using ioutil.ReadAll, you're slurping it all into memory rather than
    streaming it. That's okay if it's small, but what if it's big?

    I guess you want a Seeker? For http.ServeContent?

    Maybe you actually want
    http://golang.org/pkg/net/http/httputil/#ReverseProxy ?
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 30, '12 at 9:21p
activeDec 13, '12 at 2:48p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase