How to Convert Interface to String in Go

Sheeraz Gul Feb 02, 2024
  1. Golang Interface to String
  2. Use fmt.Sprintf to Convert the Interface to String in Golang
  3. Use Type Casting to Convert the Interface to String in Golang
How to Convert Interface to String in Go

This tutorial demonstrates how to convert the Interface to string in Golang.

Golang Interface to String

The Interface stores the set of the method without implementation in Golang, where methods in the Interface will not have the method body. In Golang, the Interface can be empty, and a value can be assigned to it by variable declaration.

Sometimes it is required to convert the Interface to string in Golang. There are two methods to convert the Interface to a string in Golang.

Use fmt.Sprintf to Convert the Interface to String in Golang

The method fmt.Sprintf can convert anything to a string in Golang. This method can also convert an Interface to a string in Golang.

Let’s see an example:

package main

import "fmt"

func main() {
	//Interface with a string value
	var Demo interface{} = "Delftstack"
	resultString := fmt.Sprintf("%v", Demo)
	fmt.Println(resultString)

	//Interface with an array value
	var Demo1 interface{} = []int{10, 20, 30, 40, 50}
	resultString1 := fmt.Sprintf("%v", Demo1)
	fmt.Println(resultString1)
}

The code above will convert the Interface with value string and array to a string. See the output:

Delftstack
[10 20 30 40 50]

Program exited.

But what if the Interface is an array with multiple values? In that case, we can create an empty slice of the length of the Interface and use a for loop to convert each value to a string and use them or put them into the slice.

See example:

package main

import "fmt"

func main() {
	Demo := []interface{}{
		"Delftstack",
		10, 20, 30,
		[]int{6, 5, 9},
		struct{ A, B int }{10, 20},
	}

	// Print the interface content
	fmt.Println(Demo)

	// Print the original type before conversion
	fmt.Printf("Original Type of interface: %T\n", Demo)

	// now create an empty slice based on the length of the Interface
	emptyslice := make([]string, len(Demo))

	// Iterate over the Interface and convert values to string
	for index, value := range Demo {
		emptyslice[index] = fmt.Sprint(value)
	}

	// Print the content of the slice
	fmt.Println(emptyslice)

	// Print the type after conversion
	fmt.Printf("The new type is: %T\n", emptyslice)
	fmt.Printf("%q\n", emptyslice)
}

The code above will convert the Interface with multiple values to strings. See output:

[Delftstack 10 20 30 [6 5 9] {10 20}]
Original Type of interface: []interface {}
[Delftstack 10 20 30 [6 5 9] {10 20}]
The new type is: []string
["Delftstack" "10" "20" "30" "[6 5 9]" "{10 20}"]

Program exited.

Use Type Casting to Convert the Interface to String in Golang

Type casting is also possible in Golang. We can type cast an interface to a string; the process is very simple where the syntax .(string) is used to type cast anything to a string.

Let’s try an example:

package main

import "fmt"

func main() {
	//Interface with a string value
	var Demo interface{} = "Delftstack"
	resultString := Demo.(string)
	fmt.Println(resultString)

}

In this case, we can only type cast the Interface with a string value to a string. The Interface with array value cannot be type cast.

See the output:

Delftstack

Program exited.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Go String