FAQ
I want to have something like named, ordered parameters in Go, and came to
the following solution:

package main

import "fmt"

type param struct {
name, value string
}

func main() {
show([]param{{"x", "1"}, {"y", "b"}, {"z", "abc"}})
}

func show(params []param) {
for _, item := range params {
fmt.Println(item.name + ": " + item.value)
}
}

http://play.golang.org/p/MddW-Rpuyg

Simulating named parameters with show([]param{{"x", "1"}, {"y", "b"}, {"z",
"abc"}}) looks ok, even though I'd prefer show(x="1", y="b", z="abc") like
in Python. I tried a map, which has a litte bit more concise syntax, but
lacks a defined order. Any better idea?

In real life the values are not always strings and have to be converted
therefore. I tried interface{}, but that didn't help because the format
string depends on the actual type. Then I tried fmt.Stringer interface, but
that ist not implement by string, int and other fundamental types. Is there
a way to handle this?

--

Search Discussions

  • Christian at Nov 28, 2012 at 2:02 pm
    I want to have something like named, ordered parameters in Go, and came to
    the following solution:

    package main

    import "fmt"

    type param struct {
    name, value string
    }

    func main() {
    show([]param{{"x", "1"}, {"y", "b"}, {"z", "abc"}})
    }

    func show(params []param) {
    for _, item := range params {
    fmt.Println(item.name + ": " + item.value)
    }
    }

    http://play.golang.org/p/MddW-Rpuyg

    Simulating named parameters with show([]param{{"x", "1"}, {"y", "b"}, {"z",
    "abc"}}) looks ok, even though I'd prefer show(x="1", y="b", z="abc") like
    in Python. I tried a map, which has a litte bit more concise syntax, but
    lacks a defined order. Any better idea?

    In real life the values are not always strings and have to be converted
    therefore. I tried interface{}, but that didn't help because the format
    string depends on the actual type. Then I tried fmt.Stringer interface, but
    that ist not implement by string, int and other fundamental types. Is there
    a way to handle this?

    --
  • Chris dollin at Nov 28, 2012 at 2:11 pm

    On 28 November 2012 08:33, Christian wrote:
    I want to have something like named, ordered parameters in Go,
    Is this just a "want" or was there some problem you had that named,
    ordered parameters would solve?

    Because once one has noticed ... arguments and explicit struct arguments
    and map arguments, there doesn't seem to be much demand for such a feature.

    Chris

    --
    Chris "allusive" Dollin

    --
  • Jan Mercl at Nov 28, 2012 at 2:11 pm

    On Wed, Nov 28, 2012 at 9:07 AM, Christian wrote:
    I want to have something like named, ordered parameters in Go, and came to
    the following solution:
    The approach sacrifies both execution speed and compilation time type
    safety checks. What is the reason for it?

    -j

    --
  • André Moraes at Nov 28, 2012 at 2:52 pm

    On Wed, Nov 28, 2012 at 6:07 AM, Christian wrote:
    I want to have something like named, ordered parameters in Go, and came to
    the following solution:
    This solve the problem without adding language features (they are
    blocked until 2.0 anyway) and you still have fast compilation, fast
    execution, type-safety and a consistent API.

    package main

    import "fmt"

    type MyArgs struct {
    Arg1 int
    Arg2 int
    Arg3 int
    }
    func fn1(data MyArgs) {
    fmt.Printf("data: %v\n", data)
    }

    func main() {
    fn1(MyArgs{Arg1: 10})
    }



    --
    André Moraes
    http://amoraes.info

    --
  • DisposaBoy at Nov 29, 2012 at 7:23 am
    sounds like your function does too much . if you really need this feature and you can't split your function up into smaller ones then create different functions with appropriate names that call your function with appropriate defaults

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 28, '12 at 2:02p
activeNov 29, '12 at 7:23a
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase