Go: Convert interface to string

Use fmt.Sprintf to convert an interface value to a string:

var x interface{} = "abc"
str := fmt.Sprintf("%v", x)

In fact, the same technique can be used to get a string representation of any data structure:

var x interface{} = []int{1, 2, 3}
str := fmt.Sprintf("%v", x)
// Output: [1 2 3]


Be the first to comment!