How to provide string formatting for structs?

go, string, struct

Solution

You can use %v instead of %s. %s will assume a string, something that can be converted to a string (i.e. byte array) or an object with a String() method. Using %v will check the type and display it correctly.

Example of the String() method call with %s with your example: http://play.golang.org/p/bxE91IaVKj

Problem

I've a struct called item ``` type Item struct { Limit int Skip int Fields string } item := Item { Limit: 3, Skip: 5, Fields: "Valuie", } ``` how could I get the field name, value and join it into a string. something like: ``` item := Item { Limit: 3, Skip: 5, Fields: "Valuie", } ``` to a string something like ``` "Limit=3&Skip=5&Fields=Valuie" ``` And I've try reflections to get convert interface to field value map so far. Am I going the right way? Cause I think there might have some better solutions. And thanks! ``` m, _ = reflections.Items(data) for k, v := range m { fmt.Printf("%s : %s\n", k, v) } ``` I've got ``` Limit : %!s(int=3) Skip : %!s(int=5) Fields : Valuie ```

Original source