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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度