fmt.Fprint and os.Stdout.Write

6,566 views
Skip to first unread message

Archos

unread,
Oct 18, 2010, 4:21:22 AM10/18/10
to golang-nuts
Is there any difference between to use `fmt.Fprint(os.Stdout, "str")`
and `os.Stdout.Write([]byte("str"))` ?

I'm using now `os.Stdout.Write` to write ANSI terminal escape controls.

Andrew Gerrand

unread,
Oct 18, 2010, 5:34:33 AM10/18/10
to Archos, golang-nuts

If all you want to do is output specific byte strings (and not do any
string formatting) you can just write straight to os.Stdout. fmt will
do the same thing eventually, so if you don't need any of its
functionality you might as well skip it.

Andrew

chris dollin

unread,
Oct 18, 2010, 5:39:31 AM10/18/10
to Archos, golang-nuts
On 18 October 2010 09:21, Archos <raul...@sent.com> wrote:
> Is there any difference between to use `fmt.Fprint(os.Stdout, "str")`
> and `os.Stdout.Write([]byte("str"))` ?

Yes. fmt.Fprint will interpret %-verbs in the string, so if you might
have %-characters in that string, beware.

[I assume your question was more general than just for the specific
string "str".]

Chris

--
Chris "allusive" Dollin

Benny Siegert

unread,
Oct 18, 2010, 8:14:15 AM10/18/10
to chris dollin, golan...@googlegroups.com
> Yes. fmt.Fprint will interpret %-verbs in the string, so if you might
> have %-characters in that string, beware.

No. Fprint does not use format strings, Fprintf does.

--Benny.

--
The first essential in chemistry is that you should perform practical
work and conduct experiments, for he who performs not practical work
nor makes experiments will never attain the least degree of mastery.
        -- Abu Musa Jabir ibn Hayyan (721-815)

chris dollin

unread,
Oct 18, 2010, 8:20:50 AM10/18/10
to Benny Siegert, golan...@googlegroups.com
On 18 October 2010 13:14, Benny Siegert <bsie...@gmail.com> wrote:
>> Yes. fmt.Fprint will interpret %-verbs in the string, so if you might
>> have %-characters in that string, beware.
>
> No. Fprint does not use format strings, Fprintf does.

(fx:peers-closely)

Dagnabbit. So it isn't.

Thanks for catching it.

Ostsol

unread,
Oct 18, 2010, 9:04:24 AM10/18/10
to golang-nuts
Fprint can print any data type, while Stdout.Write only prints byte
slices.

-Daniel
Reply all
Reply to author
Forward
0 new messages