Convert interface to string

yourbasic.org/golang

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

How to format with fmt

Share this page: