any way to ignore multiple return values?

8,613 views
Skip to first unread message

Mark Ivey

unread,
May 11, 2011, 11:27:22 AM5/11/11
to golang-nuts
I want to use path.Split() to calculate the default value for a flag.
This code works, but puts "dir" in the global namespace:

var dir, _ string = path.Split(os.Args[0])
var imageDir = flag.String("images",
path.Join(dir, "images"),
"Directory containing images")

I'd like to do this without "dir" variable by putting the path.Split()
call directly in the arguments to path.Join(), but I can't figure out
how to ignore the second return value.

Here's what I would do in Python, but this doesn't work in Go
(compiler says: multiple-value path.Split() in single-value context):

var imageDir = flag.String("images",
path.Join(path.Split(os.Args[0])[0], "images"),
"Directory containing images")

What's the right way to do this in Go?

Russ Cox

unread,
May 11, 2011, 11:33:30 AM5/11/11
to Mark Ivey, golang-nuts

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

David Symonds

unread,
May 11, 2011, 11:34:45 AM5/11/11
to Mark Ivey, golang-nuts
On Wed, May 11, 2011 at 8:27 AM, Mark Ivey
<con...@countlessprojects.com> wrote:

> 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.

David Roundy

unread,
May 11, 2011, 12:32:14 PM5/11/11
to Russ Cox, Mark Ivey, golang-nuts
On Wed, May 11, 2011 at 8:33 AM, Russ Cox <r...@google.com> wrote:
> 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.

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

Steven

unread,
May 11, 2011, 12:49:08 PM5/11/11
to David Roundy, Russ Cox, Mark Ivey, golang-nuts
Or you could use an init function:

var imageDir *string
func init() {
  dir, _ := path.Split(os.Args[0])
  imageDir = flag.String("images", path.Join(dir, "images"), "Directory containing images")
}

Tim Schaeffer

unread,
May 11, 2011, 3:09:04 PM5/11/11
to golan...@googlegroups.com, Mark Ivey

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.

var dir string = first(path.Split(os.Args[0])).(string)



Tim Schaeffer

unread,
May 11, 2011, 3:39:44 PM5/11/11
to golan...@googlegroups.com, Mark Ivey
On Wednesday, May 11, 2011 3:09:04 PM UTC-4, Tim Schaeffer wrote: 
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.

Well, more to the point of the OP:

var imageDir = flag.String("images", 
                           path.Join(first(path.Split(os.Args[0])).(string), "images"), 
                           "Directory containing images")


Mark Ivey

unread,
May 11, 2011, 11:24:44 PM5/11/11
to David Symonds, golang-nuts
I ended up taking a similar approach after realizing that the first thing I originally looked for was path.DirName:

func dirname(pathIn string) string {
  dir, _ := path.Split(pathIn)
  return dir
}

var imageDir = flag.String("images",
   path.Join(dirname(os.Args[0]), "images"),
   "Directory containing images")

Thanks for all the answers!
Reply all
Reply to author
Forward
0 new messages