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