将字符串切片转换为字符串?

huangapple go评论68阅读模式
英文:

String slice to string?

问题

我正在编写一个程序,它接受普通的标志和一个单独的“非标志”参数,就像这样:

my-prog -level 1 map.txt

为了处理 map.txt,我使用 flag.Args()[:1],但是如果将其传递给需要一个字符串的函数,就会出现编译错误:

cannot use flag.Args()[:1] (type []string) as type string in assignment

如何将 flag.Args()[:1] 转换为字符串?

英文:

I'm writing a program that takes normal flags and a single "non-flag" argument, like this:

my-prog -level 1 map.txt

To process map.txt I use flag.Args()[:1], but if you pass that to functions that takes a string, this compiler error appears:

cannot use flag.Args()[:1] (type []string) as type string in assignment

How can I convert flag.Args()[:1] to a string?

答案1

得分: 1

将第一个string索引而不是切片它:flag.Args()[0]

或者你可以使用flag.Arg(0)函数只获取第一个参数。

英文:

Index the first string instead of slicing it: flag.Args()[0]

Or you can use the flag.Arg(0) function to get the first argument only.

huangapple
  • 本文由 发表于 2016年9月15日 21:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/39512995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定