6

Convert interface to string

 3 years ago
source link: https://yourbasic.org/golang/interface-to-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

format-thumb.jpg

Top fmt formatting tips

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK