Printing slice without the '[' ']'

8,542 views
Skip to first unread message

Tony Worm

unread,
Aug 7, 2013, 11:07:01 AM8/7/13
to golan...@googlegroups.com
Is there go syntax for printing slices without the brackets?

Brad Fitzpatrick

unread,
Aug 7, 2013, 11:12:31 AM8/7/13
to Tony Worm, golang-nuts


On Wed, Aug 7, 2013 at 8:07 AM, Tony Worm <verd...@gmail.com> wrote:
Is there go syntax for printing slices without the brackets?

--
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.
 
 

Tony Worm

unread,
Aug 7, 2013, 11:27:11 AM8/7/13
to golan...@googlegroups.com
helper function which could be modified to specify formatting

David DENG

unread,
Aug 7, 2013, 12:42:12 PM8/7/13
to golan...@googlegroups.com

Tony Worm

unread,
Aug 7, 2013, 1:48:47 PM8/7/13
to golan...@googlegroups.com
My aim is to reduce the number of calls to fmt.----

I think that should reduce the number of i/o events and context switches
which is the real aim

printing in a loop doesn't address the original question of a Go syntax, which it seems there isn't one. I was hoping something of the varidic variety existed

Dave Cheney

unread,
Aug 7, 2013, 7:29:28 PM8/7/13
to Tony Worm, golang-nuts
> My aim is to reduce the number of calls to fmt.----
>
> I think that should reduce the number of i/o events and context switches
> which is the real aim

Those two operations sound unrelated. Do you need to use a buffered writer ?

Tony Worm

unread,
Aug 7, 2013, 7:47:15 PM8/7/13
to Dave Cheney, golang-nuts
calls to printf and scanf involve asking the operating system to do something on your behalf as I recall. I don't see how Go could do it otherwise. 

Imagine printing 1MM floats with 1MM printf's...

I'd guess there is some buffering going on somewhere,
but I recall it being faster to build large strings in memory
and then print that out with one call to printf

Anyhow, I was originally looking for some sweet gopher magic on the syntax side of this.

btw, I heard that Go's syntax is in part based on the Limbo scripting language from Inferno

David DENG

unread,
Aug 7, 2013, 8:12:01 PM8/7/13
to golan...@googlegroups.com
Calling to fmt.Printxxxx is always expensive. For most of the time, you needn't optimize it.

David

Jens Alfke

unread,
Aug 7, 2013, 8:50:07 PM8/7/13
to golan...@googlegroups.com, Dave Cheney


On Wednesday, August 7, 2013 4:47:15 PM UTC-7, Tony Worm wrote:
calls to printf and scanf involve asking the operating system to do something on your behalf as I recall. I don't see how Go could do it otherwise. 

I haven't looked at the implementation of Printf, but there's usually buffering in between. It's entirely possible that Printf buffers the entire output in memory and then makes one Write call. Or maybe it buffers in 32k chunks or something. And at a higher level, the output streams might buffer in memory before writing to the OS's streams.
 
I'd guess there is some buffering going on somewhere,
but I recall it being faster to build large strings in memory
and then print that out with one call to printf


I think you're over-optimizing. Do it in a way that makes sense to you, then if it's too slow you can profile it and find out where the bottlenecks are. As David said, formatted IO is pretty CPU-intensive to begin with.

--Jens

andrey mirtchovski

unread,
Aug 7, 2013, 8:54:53 PM8/7/13
to golang-nuts
anything you do with fmt (and consequently reflection) will be much
more computationally expensive than reslicing the result to
[1:len()-1] to remove the square brackets.

btw, limbo is not a scripting language.

cheers!

Rob Pike

unread,
Aug 7, 2013, 9:14:55 PM8/7/13
to andrey mirtchovski, golang-nuts
It's not documented but yes, Printf etc. do a single write system call for each call, which means they buffer it all up first. (I have been bothered too many times in the past by C's printf intertwingling my output.)

Perhaps this should be documented, or perhaps it should not, so we can flush partial results to avoid blowups.

-rob

Reply all
Reply to author
Forward
0 new messages