duandai3964 2017-10-10 18:37
浏览 121
已采纳

在GoLang CLI程序中传递长参数的最佳方法及其局限性

I am writing a CLI program in GO and need to be able to pass a rather long list of arguments to the main function. At the moment I am using the standard way of passing arguments (space separated values) and then retrieve 'em using os.Args[index].

So my questions are:

  1. Is there a limit to the number of arguments one can pass?
  2. Is there a limit to the length of a string argument?
  3. Is there any other way of achieving this and if so how?
  • 写回答

1条回答 默认 最新

  • dongxingqiu7943 2017-10-10 18:47
    关注
    1. No, as far as go is concerned. Command line args are parsed into a []string, so at this level it's just a matter of how much memory is available. But, the comments below are correct. Your system will have its own lower limits that you'll hit first.
    2. Same as above.
    3. If you find yourself passing tons for args for every run:
      • if they don't change much, do they make sense as sane defaults for your flags?
      • if they don't make sense as defaults, consider moving to reading them from a config file. CSV, YAML, whatever.

    Without knowing more, I'd recommend a the config file. They make it easy to track different configs/runs, easier to edit than a long list of cmd line args, and you can always write it so that args on the cmd line overwrite values in the config.

    Check out awesome-go for libraries that already do this, specifically under the command-line and configuration sections.

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算