Re: Expanding slices as arguments

4,941 views
Skip to first unread message

Aiden

unread,
Dec 18, 2012, 9:32:44 AM12/18/12
to golan...@googlegroups.com
Found https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/d_jPf74RMCo, is using reflection still the
recommended approach?

On Tuesday, 18 December 2012 14:23:38 UTC, Aiden wrote:

Hi all,

Tried Googling, but can't find much ...

With

func Command(name string, arg ...string) *Cmd

Is there a way to pass a slice as the second parameter? I have an set of arguments for a command.

Cheers!

Peter

unread,
Dec 18, 2012, 9:37:29 AM12/18/12
to golan...@googlegroups.com

Igor Cananea

unread,
Dec 18, 2012, 9:37:56 AM12/18/12
to Aiden, golan...@googlegroups.com
This works: http://play.golang.org/p/zWODNmZjvH

    Igor Chaves Cananéa

"Designing grand concepts is fun; finding nitty little bugs is work." (Frederick P. Brooks Jr - The Mythical Man-Month)
Visitem meus blogs! http://igorbh.blogspot.com e http://nerdpensando.blogspot.com/



--
 
 

Aiden

unread,
Dec 18, 2012, 9:38:47 AM12/18/12
to golan...@googlegroups.com
Thanks Peter, looks like what im after :D

Aiden

unread,
Dec 18, 2012, 9:41:29 AM12/18/12
to golan...@googlegroups.com, Aiden
Nice example, Thanks :)
Didn't think I came across this reading effective ... might be worth popping in there?

minux

unread,
Dec 18, 2012, 9:46:20 AM12/18/12
to Aiden, golan...@googlegroups.com
On Tue, Dec 18, 2012 at 10:41 PM, Aiden <aide...@gmail.com> wrote:
Nice example, Thanks :)
Didn't think I came across this reading effective ... might be worth popping in there?
the introduction section of effective Go says:

> This document gives tips for writing clear, idiomatic Go code. It augments the language specification, 
> the Tour of Go, and How to Write Go Code, all of which you should read first.

which means that, you really should read the language specification before effective Go.

Peter

unread,
Dec 18, 2012, 9:54:02 AM12/18/12
to golan...@googlegroups.com, Aiden
Well perhaps that needs looking at. Go's spec, whilst far simpler than many other languages', is still quite a long document. Effective Go is certainly not a subset, but it does cover much of the same material - someone who has read the spec does not need to be told "The control structures of Go are related to those of C but differ in important ways".

It's great that the definitive spec is so accessible, but I don't see that it should be read before "Effective Go".

Patrick Mylund Nielsen

unread,
Dec 18, 2012, 9:56:16 AM12/18/12
to Peter, golang-nuts, Aiden
Agreed. Effective is read after the introduction, whether or not it was supposed to be.


--
 
 

minux

unread,
Dec 18, 2012, 10:14:39 AM12/18/12
to Peter, golan...@googlegroups.com, Aiden
On Tue, Dec 18, 2012 at 10:54 PM, Peter <peter.a...@gmail.com> wrote:
Well perhaps that needs looking at. Go's spec, whilst far simpler than many other languages', is still quite a long document. Effective Go is certainly not a subset, but it does cover much of the same material - someone who has read the spec does not need to be told "The control structures of Go are related to those of C but differ in important ways".

It's great that the definitive spec is so accessible, but I don't see that it should be read before "Effective Go".
I just skimmed through effective Go document, and i think using ... to pass a slice to
...interface{} parameter is documented and explained.

Within the function Printfv acts like a variable of type []interface{} but if it is passed to another variadic function, it acts like a regular list of arguments. Here is the implementation of the function log.Println we used above. It passes its arguments directly to fmt.Sprintln for the actual formatting.


// Println prints to the standard logger in the manner of fmt.Println.
func Println(v ...interface{}) {
    std.Output(2, fmt.Sprintln(v...))  // Output takes parameters (int, string)
}

We write ... after v in the nested call to Sprintln to tell the compiler to treat v as a list of arguments; otherwise it would just pass v as a single slice argument.

Reply all
Reply to author
Forward
0 new messages