How to convert an interface to a string in Golang

Last updated on Sep 13, 2022 by Suraj Sharma



In this tutorial, you'll learn to convert a golang interface to a string


The fmt.Sprintf() built-in function is used to convert an interface to a golang string.


The verb '%v' is used for interface as '%d' is used for Integer of base 10.


Example


  package main

  import "fmt"

  func main () {
    var val interface{}
    var str string
    val = []string{"go", "programming", "language"}
    str = fmt.Sprintf(val)

    fmt.Printf(str) // "[go programming language]"
  }


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.