There's no syntax to do the equivalent of Python's [0].
In this case though, I would just write
func defaultImageDir() string {
...
}
and then use defaultImageDir() in the argument to flag.String.
Russ
> What's the right way to do this in Go?
Make a little helper function and use it as the default value in the flag.
func defaultImageDir() string {
dir, _ := path.Split(os.Args[0])
return path.Join(dir, "images")
}
Dave.
Or if you were likely to only use this function once, just use a
function literal:
var imageDir = function() *string {
dir, _ := path.Split(os.Args[0])
return flag.String("images", path.Join(dir, "images"), "Directory
containing images")
}()
This looks nicer to me if you never want to use defaultImageDir elsewhere.
--
David Roundy
You could also be more abstract and create some higher-order functions.
This solves the problem more generally:
func make_nth (n int) (func(a... interface{}) interface{}) {
return func(args... interface{}) interface{}{
return args[n]
}
}
var first = make_nth(0)
var second = make_nth(1)
var third = make_nth(2)// etc.