Passing variable type to function with flag

I have multiple variables of a specific type (see below):

var gw1 = &customType{
	Value1:   "x",
	Value2: "y",

var gw2 = &customType{
	Value1:   "a",
	Value2: "b",

That are passed to the following function:

func create(instance *customType) {
	value, _, err := client(instance)
	if err != nil {
	fmt.Printf("%v", value)

Function create(gw1) works as expected, but what I would like to do is pass the variable to create() via command line arguments with flags or os.Args.

So the final program would be utilized like, program -flag gw{1|2}.

It seems like with flag I can only pass very specific types (string, int, bool, etc).

I’ve thought that maybe the issue is in how the function input type is being defined?

The flag package supports creating your own flags that know how to unmarshal themselves from strings. However the process is quite complex, so unless you are planning on supporting many many incoming arguments, it might be simpler to hard code the values of gw1 and 2 yourself. Or as the type is always the same, therefore it’s values are always the same you can do something like this.

var gw customType
flag.StringVar(&gw.Value1, “a”, “default”, “a’s description”)

That doesn’t quite work, because I can’t pass a string variable to the function as it’s been defined to take a non-string argument.

But I believe you are right, in that I will have to hard-code the values.

I’m not sure what you mean. gw is of type customType.

I didn’t explain this very well from the start. I’m working with Digital Ocean’s GO API, and the customType is actually DropletCreateRequest as defined here.

I created my variables of this type like (of which there are multiple of the same type):

var server = &godo.DropletCreateRequest{
	Name:   "servername",
	Region: "nyc3",
	Size:   "512mb",
	Image: godo.DropletCreateImage{
		Slug: "freebsd-11-0-x64-zfs",
	SSHKeys: sshkey,
	IPv6:    true,
	Tags:    []string{"servertag"},

For a fucntion like:

func create(instance *godo.DropletCreateRequest) {
	droplet, _, err := client.Droplets.Create(instance)
	if err != nil {
	fmt.Printf("%v", droplet)

To be called like:

func main()
   // some conditional logic with flags

So what I pass to create() has to be of type godo.DropletCreateRequest because of the way the function was created (or so I think anyway).

There are already Go tools for this sort of operation, using this API, but I thought this would make a good first project :slight_smile:

What about this

var server godo.DropletCreateRequest
flag.StringVar(&server.Name, "n", "", "server name")
flag.StringVar(&server.Region, "r", "nyc3", "region") // defaults to nyc3
flag.StringVar(&Server.Image.Slug, "i", "freebsd-11-0-x64-zfs", "image") // defaults to zfs
// more flags

So then I would create a droplet with command line flags, like program --n=myServerName? And if I wanted to create outside of the defaults, it would be program --n=myServerName --r=nyc2 --i=ubuntu-14-04-x64 etc?

I like it :thumbsup:

The only caveat I see here, are long commands that will eventually be wrapped in shell scripts for fast calling, but honestly, that’s probably better than having the droplet specs baked into the program itself.
My original plan was to source static json files as arguments, but that will be down the line.

Thank you for your help.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.