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)
fmt.Println(str) // "[1 2 3]"

Fmt cheat sheet

Top fmt formatting tips

Share this page: