A proposal: Simplification of `map[string]interface{}` with Assumptive MapsWe write a lot of Go code, and most of it seems to be `map[string]interface{}`. Therefore we propose a new language feature called "Assumptive Maps".
What do they look like?Instead of
- mymap := map[string]string{"Name":"Mat"}
and
- var mymap map[int]string := map[int]string{1: "Mat", 2: "Tyler"}
we could write:
- mymap1 := {"Name": "Mat"}
- /* mymap1 would be of type map[string]interface{} or
- map[string]string depending on the options below */
- var mymap2 map[int]string = {1: "Mat", 2: "Tyler"}
- /* mymap2 would be of type map[int]string - since the developer
- has explicitally assigned a type to the lvalue */
What are they?An assumptive map is a compile-time shortcut for defining a map. At compile time, the type of the map is determined by examining the values assigned to the map.Selecting types
- Option one
- The types can be determined by the content of the shortcut map. For example, if all keys are string we know it'll be `map[string]...`. If all values are `int` the compiler knows it will be `map[string]int`.
- The compiler would throw an error if there are type mismatches, and force the developer to make a decision.
- Option two
- Another option, is that everything defined with `{...}` assumptive syntax always becomes `map[string]interface{}`, unless the lvalue already has a type defined.
- This provides complete consistency and means that maps won't change type depending on the data. If the user were to pass a value of a type other than string as the key, the compiler would issue an error.
- However, if the user already has a variable with a specific type of map, the `{...}` syntax would use that type instead of just assuming `map[string]interface{}`.
- var mymap map[int]string
- mymap = {1: "Mat", 2: "Tyler"}
- /* this is OK */
- If the user were to specify a value of a type other than one defined (or assumed) as the key, the compiler would issue an error.
- mymap := {1: "Mat", 2: "Tyler"}
- /* this would be an error, because map[string]interface{} is
- assumed by default */
Pros
- Easier to type and read
- Familiar; especially since it's so similar to JSON
Cons
- The code is potentially unclear and would require outside knowledge of the feature
Other ideas
- Something similar could be done for arrays of maps; `[{"name":"Mat"},{"name":"Tyler"}]`
One trick I've used in the past to save typing in this kind of situation is to define a type.
Eg type m map[string]interface{}
Then you can write m{"foo": 5} and or is assignment-compatible with map[string]interface{}.
It's not flawless (when they're nested, something might be confused by the m type), but it's a useful technique and good in other situations too.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
m := {a: b, c: d}
A proposal: Simplification of `map[string]interface{}` with Assumptive Maps