duanlie7962 2019-01-02 15:43
浏览 17
已采纳

是否可以将可变参数与标志包用法结合使用? [关闭]

I want my function to take optional arguments in an efficent way.

Reading similar posts led me to variadic args and I'm trying to implement it alongside the flag package (simply looking for any alternative to the users being able to run available command line flags of their choice.. This is my flag package usage:

func main() {
    var target string
    var method string
    flag.StringVar(&target, "target", "http://google.com/robots.txt", "Target address")
    flag.StringVar(&method, "method", "GET", "Method")
    flag.Parse()

    requests.MakeRequest(target, method)
}

This is an example of variadic args function:

func foo(params ...int) {
    fmt.Println(len(params))
}

func main() {
    foo()
    foo(1)
    foo(1, 2, 3)
}

Can I combine the two? If this is not possible - how can I pass main program's user given arguments to a variadic function then?

  • 写回答

1条回答 默认 最新

  • dousi7579 2019-01-02 17:05
    关注

    Your question shows a misunderstanding, what variadic arguments are for.

    As you realized, variadics are used, when the amount of arguments to a function varies. You try to use this concept to hand over parsed commandline arguments of a variable amount. This is a bad idea in many ways. The obvious here is, that you are coupling your commandline arguments with the distribution of the flags and arguments in your code. Another is, that the variadic arguments loose the positional information. How do you expect to identify which 4 of the 5 arguments you have received?

    You should define a struct to hold the flags for your code and write a commandline parser, that fills or creates this struct from defaults and given commandline options. This struct is then used to provide the flags and options throughout your application.

    In practice, this could look like this:

    type options struct {
        target string
        method string
    }
    
    
    func main() {
        config := parseCommandLine()
        theRealStuffHappensHere(config)
    }
    
    func theRealStuffHappensHere(config options) {
        if config.method == "GET" {
        // ...
        }
    }
    
    func parseCommandLine() options {
        var config options
        flag.StringVar(&(config.target), "target", "http://google.com/robotstxt", "Target address")
        flag.StringVar(&(config.method), "method", "GET", "Method")
        flag.Parse()
    
        return config
    }
    

    The variadic arguments are heavily used in the fmt package, where the amount of required arguments depends on the amount of placeholders in the format string. Your usecase does not match.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错