FAQ

On Thursday, July 24, 2014 9:17:28 AM UTC+2, Frits van Bommel wrote:
I think you're looking for Value.MapKeys()
<http://golang.org/src/pkg/reflect/value.go?s=37314:37346#L1219>.
Sorry, better link: Value.MapKeys()
<http://golang.org/pkg/reflect/#Value.MapKeys> (documentation, not source
code)

Here's a full example that does what I think you're trying to do:
http://play.golang.org/p/yi1jHyEqqq
Note the use of k.Interface() and v.Interface() to get interface{} values
that fmt can handle properly, and %v in the format string because they
might not be string-like types.

It's still not ideal code (bytes.Buffer
<http://golang.org/pkg/bytes/#Buffer> is your friend, repeated string
concatenations are usually slower) and you could even use encoding/xml to
do what you want:
   http://play.golang.org/p/8t3_KUboYk

Also, it's usually better to use the site linked above (play.golang.org)
for code examples, especially relatively large ones like yours.
On Thursday, July 24, 2014 9:30:27 AM UTC+2, Theeraphol Wattanavekin wrote:

I am also trying to do type assertion for every case as below but also get
the error as well:

func serialize(value interface{}) string {
result := "<value>"
switch asserted_value := value.(type) {

[snip other cases]
case map[string]string, map[string]int, map[string]int8,
map[string]int16, map[string]int32, map[string]int64,
map[string]uint, map[string]uint8, map[string]uint16,
map[string]uint32, map[string]uint64,

map[string]float32, map[string]float64:
result += "<struct>"
for k, v := range asserted_value { << -- error here "cannot range
over asserted_value (type interface {})"
That won't work because you specified multiple types. asserted_value has
the asserted type only in cases with a *single* type. In other cases it's
just the original interface type asserted on.

result += "<member>"
result += fmt.Sprintf("<name>%s</name>", k)
result += serialize(v)
result += "</member>"
}
result += "</struct>"

default:
log.Fatal("Cannot serialise: ", value)
}
result += "</value>"
return result
}

--
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/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 5 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 24, '14 at 7:07a
activeJul 24, '14 at 8:01a
posts5
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase