FAQ

On 10 July 2013 13:47, Louisa Berger wrote:

This is not just a map of strings : I don't want ints or the like
converted to strings.
Does anyone know how to check if an interface field is either a string or
any kind of (type X string) ?
So if I understand you correctly, there are an arbitrary number of named
types like A whose underlying type is string, and you don't know them a
priori?

In that case use reflection:

m := map[string]interface{}{"a" : A("string field")}
m2 := make(map[string]interface{}{"a" : "string field"})
for k, v := range m {
     if reflect.TypeOf(v).Kind() == reflect.String {
       m2[k] = string(v)
     } else {
       m2[k] = v
     }
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 10, '13 at 2:42a
activeJul 11, '13 at 7:42a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase