Go Error:“多值文件路径.Glob()在单值上下文中” [英] Go Error: "multiple-value filepath.Glob() in single-value context"

查看:139
本文介绍了Go Error:“多值文件路径.Glob()在单值上下文中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么这行代码:

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

生成这些错误:

 单值上下文中的多值os.Getwd()
单值上下文中的多值filepath.Glob()

谢谢!
Bryan

解决方案

两者都返回错误,因此您无法直接指定它们。
$ b

  func Glob(模式字符串)(匹配[]字符串,错误错误)
func Getwd()(dir字符串,错误错误)

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

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

使用:

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

但最好的做法是检查错误并采取行动't 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 /#必须



  func必须(t *模板,错误错误)*模板




必须是一个辅助函数,它包含对返回的函数的调用(* Template,error),如果错误不为零,则发生混乱。

它用于变量初始化,例如:



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


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

解决方案

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"))

这篇关于Go Error:“多值文件路径.Glob()在单值上下文中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆