php 获取 用户输入参数,go-读取输入参数

前言

编写程序的时候,需要读取用户输入的参数来做一些事情。在golang里面有两个库可以来制作这块的功能。

flag

flag包,加载输入信息。官方两个例子:

// Example 1: A single string flag called "species" with default value "gopher".

var species = flag.String("species", "gopher", "the species we are studying")

// Example 2: Two flags sharing a variable, so we can have a shorthand.

// The order of initialization is undefined, so make sure both use the

// same default value. They must be set up with an init function.

var gopherType string

func init() {

const (

defaultGopher = "pocket"

usage = "the variety of gopher"

)

flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)

flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")

}

// PrintDefaults prints, to standard error unless configured otherwise,

// a usage message showing the default settings of all defined

// command-line flags.

// For an integer valued flag x, the default output has the form

// -x int

// usage-message-for-x (default 7)

// The usage message will appear on a separate line for anything but

// a bool flag with a one-byte name. For bool flags, the type is

// omitted and if the flag name is one byte the usage message appears

// on the same line. The parenthetical default is omitted if the

// default is the zero value for the type. The listed type, here int,

// can be changed by placing a back-quoted name in the flag's usage

// string; the first such item in the message is taken to be a parameter

// name to show in the message and the back quotes are stripped from

// the message when displayed. For instance, given

// flag.String("I", "", "search `directory` for include files")

// the output will be

// -I directory

// search directory for include files.

//

// To change the destination for flag messages, call CommandLine.SetOutput.

func PrintDefaults() {

CommandLine.PrintDefaults()

}

参考资料

有疑问加站长微信联系(非本文作者)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值