0

In Golang, the values in map, can they be types ? For example how do I create a map m[string]type such that it can be like this,

 m["abc"] = int
 m["def"] = string
 m["ghi"] = structtype ( some structure of type structtype)

I need such map because, I have a function which has a string argument and according to that string argument the function creates a variable of a certain type and does some operations. So, if I have a map which maps a string to a type, the function can check that map using the string argument as the key to find out which type of variable it needs to create.

7
  • 1
    types are not values, but you can use a zero value of a type, or reflect.Type values. Commented Jul 27, 2017 at 20:43
  • 1
    You will need to become very familiar with the reflect package. Commented Jul 27, 2017 at 20:44
  • RayfenWindspear and @JimB so the map should be defined as m[string]interface{} ? Commented Jul 27, 2017 at 20:51
  • @user1851006 map[string]reflect.Type I swear I've answered this question before, but I can't seem to find it.... Commented Jul 27, 2017 at 20:53
  • 1
    Possible duplicate of Is it possible to store a Type in a map and use it later to instantiate an object in Go lang? Commented Jul 27, 2017 at 20:56

1 Answer 1

4

I sounds like you need map[string]reflect.Type

val := map[string]reflect.Type{}{}

val["int"] = reflect.TypeOf(int(0))
pointer_to_new_item := reflect.New(val["int"])

If you need a non-pointer value you then use Indirect:

new_item := reflect.Indirect(pointer_to_new_item)

Using reflect to create a value will give you a reflect.Value, which you then need to unpack the actual value you want from using other reflect functions. See The reflect documentation for more info.

Keep in mind that reflect.New only makes simple types, structures, etc. If you need channels, maps, or slices there are other, similar functions that work like the make builtin.

Sign up to request clarification or add additional context in comments.

7 Comments

in your example, instead of int, if it is a struct type, do I have to create a random struct variable of that type to reflect it's value ?
Yes, but you only have to do it when populating the map, so it only has to be done once.
You must also note, that the code in this answer will give you a reflect.Value, not an int. There is extra reflect stuff involved to make it an actual int or struct or whatever it is.
Very true, that is one of the reasons I made sure to link to the docs. I should probably have put that in the answer. I'll edit it.
@MiloChristiansen It's the reason I flagged as a duplicate to one I answered that was the exact same question. You answer is good and nicely concise, but I wanted to make sure to link to other resources as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.