Println call ending with newline

3,188 views
Skip to first unread message

Archos

unread,
Mar 11, 2012, 8:45:15 AM3/11/12
to golang-nuts
"go vet" shows the error "Println call ends with newline" with
fmt.Println("foo\n")

So, should it be changed? to fmt.Printf("foo\n\n")

Rémy Oudompheng

unread,
Mar 11, 2012, 8:48:49 AM3/11/12
to Archos, golang-nuts

Warnings by go vet are just meant to be informational: you are
supposed to have a look to check that you didn't make a mistake, but
you might also have written that on purpose.

Rémy.

David Symonds

unread,
Mar 11, 2012, 8:48:54 AM3/11/12
to Archos, golang-nuts
fmt.Println automatically adds a \n. Just use fmt.Println("foo").


Dave.

Archos

unread,
Mar 11, 2012, 8:50:53 AM3/11/12
to golang-nuts
I know it. But when I've used in my code is because I wanted an extra
line.

minux

unread,
Mar 11, 2012, 8:55:44 AM3/11/12
to Archos, golang-nuts
On Sun, Mar 11, 2012 at 8:50 PM, Archos <raul...@sent.com> wrote:
I know it. But when I've used in my code is because I wanted an extra
line.
I think Printf("foo\n\n") will make your intention more clear.
(IMHO, everybody will doubt if you are misusing Println when encountered by 
Println("foo\n"), so it's worth the effort to write it more clearly.)
I know you can add comment about it, but I still think the best way is to make
the code document itself.

Archos

unread,
Mar 11, 2012, 8:59:17 AM3/11/12
to golang-nuts
I'll change my code. Thanks.

On Mar 11, 12:55 pm, minux <minux...@gmail.com> wrote:

Martin Geisler

unread,
Mar 11, 2012, 10:09:20 AM3/11/12
to Archos, golang-nuts
Archos <raul...@sent.com> writes:

What about

fmt.Println("foo")
fmt.Println()

That way each line of output is on its own line in the source -- I find
that quite neat.

--
Martin Geisler

Mercurial links: http://mercurial.ch/

Archos

unread,
Mar 11, 2012, 10:51:33 AM3/11/12
to golang-nuts
That are two calls to a same function so it'll take more time and
memory; i know it's little but I prefer save it using one only
function to solve it.

On Mar 11, 2:09 pm, Martin Geisler <m...@lazybytes.net> wrote:
>  application_pgp-signature_part
> < 1KViewDownload

DisposaBoy

unread,
Mar 11, 2012, 11:38:58 AM3/11/12
to golan...@googlegroups.com
use fmt.Print(...) .... if you're going to type `\n` anyway then you'v thrown away the only benefit to fmt.Println - without knowing the actual rationale, I'd guess that that's why it's complaining

DisposaBoy

unread,
Mar 11, 2012, 11:40:14 AM3/11/12
to golan...@googlegroups.com
Also, switching to fmt.Printf should, in this case warn you about using fmt.Printf without a format string

Martin Geisler

unread,
Mar 11, 2012, 12:24:15 PM3/11/12
to Archos, golang-nuts
Archos <raul...@sent.com> writes:

> On Mar 11, 2:09 pm, Martin Geisler <m...@lazybytes.net> wrote:
>> Archos <raul....@sent.com> writes:
>> > "go vet" shows the error "Println call ends with newline" with
>> >     fmt.Println("foo\n")
>>
>> > So, should it be changed? to fmt.Printf("foo\n\n")
>>
>> What about
>>
>>   fmt.Println("foo")
>>   fmt.Println()
>>
>> That way each line of output is on its own line in the source -- I
>> find that quite neat.
>
> That are two calls to a same function so it'll take more time and
> memory; i know it's little but I prefer save it using one only
> function to solve it.

Printing a newline is normally not performance critical, but maybe you
intend to print millions of them?

I can process about 800,000 calls to Println per second when piping the
output to /dev/null. If I show the output in my terminal, I can only
print about 26,000 *lines* per second and it doesn't matter if I use a
single or two Println calls to output them.

Reply all
Reply to author
Forward
0 new messages