Initialize function fields

I have a function in Go, as shown 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 field names when calling the function. However, none of the following methods worked (they cause an "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 this be done correctly? I have no problem using field names when initializing the structure, but for the functions it seems like I'm missing something.

-4
source share
2 answers

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

The closest thing you can get to what you want is to convey the structure. Create a structure type that wraps your current parameters and modifies your function to accept the value of this structure (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 should access these fields using selector in the p parameter, for example:

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

Also note that as an additional “gain” (or load), the “parameters” (which are the fields of the structure parameter) become optional and disordered: you do not have to specify a value for all fields and you can list the fields in any order.

You can also call it like this:

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

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

 Bob lives in New York. Alice lives in Washington. 

If you do not want (or cannot) change the function to accept the struct parameter, then you can leave it as is and create a new helper function that will have this struct parameter, and all this will call the original function, passing the corresponding 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 as follows:

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

See this related question: Getting method parameter names in the Golang

+6
source

There are no default options in go.

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

But if you provide a little more information about what you are trying to achieve, there may be a way to find a solution for you.

After re-reading, I think I understand what you are trying to do. In fact, you can do it the other way around and bind a function to your data like this.

 type Person struct{ Name string Location string Age int } 

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

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

Then you can call this function as follows

 p := new(Person) //intitalize your parameters p.MyFunc() 
-2
source

All Articles