go - Can I obtain a pointer to a map value in golang? -


i'm trying use go's flag package dynamically generate flagsets , collect results in map flagname -> flag value.

my code looks this:

import "flag"  fs := flag.newflagset(strings.join(commands, " "), flag.exitonerror) requiredflags := []string{"flaga", "flagb"} flags := make(map[string]string)  _, f := range requiredflags {      flags[f] = *fs.string(f, "", "")  }   

this code compiles, map never gets updated after flagset fs parsed, values of "flaga" , "flagb" both "". makes sense me; flags of type map[string]string after all, not map[string]*string. unfortunately, can't seem fix problem using pointers. i've tried every combination of referencing , dereferencing can think of , either end nil pointer dereference (runtime error) or invalid indirect (compile time error).

how can set map , flagset such map values populated after flagset parsed?

what wrong with

flags := make(map[string]*string) _, f := range requiredflags {      flags[f] = fs.string(f, "", "")  } ... println(*(flags["flaga"])) 

?


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -