1

I have a function in Go as below:

func MyFunction(name, address, nick string, age, value int) {
    // perform some operations
}

and I want to call this function with arguments ("Bob", "New York", "Builder", 30, 1000) but I would like to use the fields names while calling the function. However none of the below technique worked (they throw the 'Unresolved reference error'):

MyFunction(name = "Bob", address = "New York", nick = "Builder", age = 30, value = 1000)

MyFunction(name : "Bob", address : "New York", nick : "Builder", age : 30, value : 1000)

How should it be properly done? I have no problem to use the fields names while initializing a structure but for functions it seems I'm missing something.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ziva
  • 3,181
  • 15
  • 48
  • 80
  • 3
    You don't, because that's the way the language is specified: [Calls spec](https://golang.org/ref/spec#Calls). If you want named fields, use a struct. – JimB Jan 08 '18 at 16:09

2 Answers2

13

The spec does not allow you to specify parameter names when calling a function. You can only list the values you wish to pass as arguments. You have to specify values for all parameters (except for the variadic parameter), and you have to list them in the expected order.

The closest you can get to what you want is passing a struct. Create a struct type that wraps your current parameters, and change your function to accept a value of that struct (or a pointer to it):

type Params struct {
    name, address, nick string
    age, value          int
}

func MyFunction(p Params) {
    // perform some operations
}

func main() {
    MyFunction(Params{
        name:    "Bob",
        address: "New York",
        nick:    "Builder",
        age:     30,
        value:   1000,
    })
}

Then of course you have to refer to these fields using a selector on the p parameter, e.g.:

func MyFunction(p Params) {
    // perform some operations
    fmt.Printf("%s lives in %s.\n", p.name, p.address)
}

Also note that as an extra "gain" (or burden), the "parameters" (which are the fields of the struct parameter) become optional and unordered: you are not forced to specify a value to all the fields, and you can list the fields in any order you like.

You can also call it like this:

MyFunction(Params{
    name:    "Alice",
    address: "Washington",
})

Output of the above calls (try it on the Go Playground):

Bob lives in New York.
Alice lives in Washington.

If you don't want to (or can't) change the function to accept a struct parameter, then you may leave it as-is, and create a new, helper function which will have this struct parameter, and all it would do is call the original function, passing the appropriate fields as arguments:

func MyFunction(name, address, nick string, age, value int) {
    // perform some operations
}

func MyFunction2(p Params) {
    MyFunction(p.name, p.address, p.nick, p.age, p.value)
}

And then you can call MyFunction() indirectly like this:

MyFunction2(Params{
    name:    "Bob",
    address: "New York",
    nick:    "Builder",
    age:     30,
    value:   1000,
})

See this related question: Getting method parameter names in Golang

icza
  • 389,944
  • 63
  • 907
  • 827
-2

There is no default Parameters in go.

You just call it MyFunction("Bob", "New York", "Builder",30, 1000)

But if you provide a bit more background on what you try to achieve, there might be a way for people to find a solution for you.

After rereading i think i understand what you are trying to do. Actually you can do it the opposite way around and bind the function to your data like so.

type Person struct{
    Name string
    Location string
    Age int
}

Now to the function. Instead of declaring MyFunc()you declare it like so.

func (p *Person)MyFunc(){
    //Do whatever you want with
    //p.Age p.Location and so on
}

Then you can call this function like so

p := new(Person)
//intitalize your parameters
p.MyFunc()
Shaban Naasso
  • 307
  • 1
  • 6
  • 3
  • 1
    Question isn't asking for default parameters, they just want to call functions with the argument names in the function call (which you also can't do in Go). – Adrian Jan 08 '18 at 16:59