Newbie: Using string for io.Writer in Go

15,870 views
Skip to first unread message

sati...@gmail.com

unread,
Sep 6, 2013, 2:35:20 PM9/6/13
to golan...@googlegroups.com
I wrote a function that takes io.Writer as parameter.
The idea is to write to a file or http.

How to use the same to write to a string ?

func SomeFun(w io.Writer) {
    fmt.Fprintln(w, "Hello")
}

someFun(io.Stdout)

var s string
someFun(s) //error

Thanks,
Satish K

David Anderson

unread,
Sep 6, 2013, 8:09:30 PM9/6/13
to sati...@gmail.com, golang-nuts
Take a look at bytes.Buffer: http://golang.org/pkg/bytes/#Buffer

- Dave


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kyle Lemons

unread,
Sep 6, 2013, 8:09:32 PM9/6/13
to sati...@gmail.com, golang-nuts
var b bytes.Buffer
someFun(&b)
s := b.String()


On Fri, Sep 6, 2013 at 11:35 AM, <sati...@gmail.com> wrote:

--
Reply all
Reply to author
Forward
0 new messages