Go错误: “在单值上下文中使用多值 filepath.Glob()”

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

Go Error: "multiple-value filepath.Glob() in single-value context"

问题

请问有人可以解释一下为什么这行代码会产生以下错误吗?

var file_list []string = filepath.Glob(os.Getwd() + "/*.*")

错误信息如下:

multiple-value os.Getwd() in single-value context
multiple-value filepath.Glob() in single-value context

谢谢!
Bryan

英文:

Could someone please explain why this line of code:

var file_list []string = filepath.Glob(os.Getwd() + "/*.*")

Is generating these errors:

multiple-value os.Getwd() in single-value context
multiple-value filepath.Glob() in single-value context

Thank you!
Bryan

答案1

得分: 5

两个函数都返回错误,所以你不能直接赋值给它们。

func Glob(pattern string) (matches []string, err error)
func Getwd() (dir string, err error)

你至少需要忽略错误返回值。

var file_list []string, _ = filepath.Glob(x)

使用以下代码:

cwd, _ = os.Getwd()
x := cwd + "/*.*"

但最佳实践是检查错误并在错误不为nil时采取相应的操作。

实际上,twotwotwo评论中补充道:

不要忽略err,否则有一天你的程序将无法按预期工作,而你将不知道原因
很多时候,你希望你的函数也返回错误,而你想要的“默认”处理程序是:

if err != nil { return err }

如果错误是完全意外的,并且在遇到错误后你的程序最好的做法是退出:

if err != nil { log.Panic("error doing foo: ", err) }

我推荐使用github.com/kisielk/errcheck来捕捉错误,即使在你试图一丝不苟的时候,也很容易在早期犯错误。


如果你真的想使用返回的两个值中的第一个值,而不引入中间变量,你需要一个辅助函数

func slice(args ...interface{}) []interface{} {
    return args
}

但在你的情况下,这并没有太大帮助,因为[]interface{}不是[]string类型。


另一个辅助函数在评论中topskip提到:

也可以使用以下模式:

oneArg := must(twoArgsFunc(...))

使用一个名为must的辅助函数,否则会引发错误,例如text/template/#Must

func Must(t *Template, err error) *Template

Must是一个辅助函数,它包装了一个返回(*Template, error)的函数调用,并且如果错误不为nil则引发错误。
它用于变量初始化,例如:

var t = template.Must(template.New("name").Parse("text"))
英文:

Both are returning error, so you can't assign them directly.

func Glob(pattern string) (matches []string, err error)
func Getwd() (dir string, err error)

You need to, at minimum, ignore the error return value.

var file_list []string, _ = filepath.Glob(x)

With:

cwd, _ = os.Getwd()
x := cwd + "/*.*"

But the best practice would be to check the error and act if it isn't nil.

Actually, twotwotwo adds in the comments:

> Don't ignore err, though, or someday your program won't do what it should and you won't know why.
Many times, you want your function to return errors as well and the "default" handler you want is

if err != nil { return err }

> If an error is completely unexpected and the best thing your program can do is quit after encountering it, then:

if err != nil { log.Panic("error doing foo: ", err) }. 

> I recommend github.com/kisielk/errcheck to catch mistakes, which are easy to make early on even when you're trying to be meticulous.


If you really wanted to use the first of the two returned values, without introducing an intermediate variable, you would need an helper function:

func slice(args ...interface{}) []interface{} {
    return args
}

But that wouldn't help much in your case, since []interface is not a []string.


Another helper function is mentioned by topskip in the comments:

> One can also use the pattern:

oneArg := must(twoArgsFunc(...)) 

> with a helper function 'must' that would panic otherwise, such as text/template/#Must

func Must(t *Template, err error) *Template

> Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil.
It is intended for use in variable initializations such as:

var t = template.Must(template.New("name").Parse("text"))

huangapple
  • 本文由 发表于 2014年7月15日 00:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/24741665.html
匿名

发表评论

匿名网友

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

确定