dsy48837 2018-01-08 16:04
浏览 4
已采纳

初始化功能字段

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.

  • 写回答

2条回答 默认 最新

  • doutaoer3148 2018-01-08 16:11
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)