GeekBits

Go Convert Interface to String

In this tutorial, we will walk you through everything that you need to know about interfaces, how they work, and explore the various methods and techniques that we can use to convert them into strings.

2 min read
Go Convert Interface to String

Type conversion is one of the most fundamental and useful tasks for any practical application. In Go, the process of converting an interface to a string type requires a foundation on how interfaces work.

What is an Interface in Go?

In Go, an interface is a type that allows us to specify a set of method signatures. Any type that implements all the methods declared in the interface will implicitly implement the interface itself.

Interfaces play a fundamental role in providing polymorphims which enables differnt type to be treated interchangebly based on the shared behavior.

Go Declare Interface

Let us start with the basics, we will begin with how to define an interface in Go. We can use the type keyword followed by the anme of the interface and a set of method signatures enclosed in curly braces.

The syntax is as shown:

type iface interface {
    Method1() string
    Method2(int) bool
}

Go Implement Interface

Once we have declared an interface, we can can implement it in a given type. The type needs to define all the methods declared in the interface.

An example syntax is as shown:

type S struct {
    // Fields
}

func (m S) Method1() string {
    return "Method1 implementation"
}

func (m S) Method2(n int) bool {
    // Method2 implementation
    return true
}

Go Use Interface

We can then use interface to declare variables, create functions that accept interface as arguments, and many more.

An example is as shown:

func InterfaceFunc(i iface) {
    // Function implementation
}

var variable iface = S{}

Go Convert Interface to String - Method 1

One method that we can use to convert interface to a string is Golang type assertion. This is a technique that is capable of extracting the underlying concrete value of an interface.

func InterfaceToString(i interface{}) (string, error) {
    if str, ok := i.(string); ok {
        return str, nil
    }
    return "", errors.New("Interface does not contain a string")
}

An example is as shown:

var iface interface{} = "Hello, World!"
str, err := InterfaceToString(iface)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println(str)
}

Output:

Hello, World!

Go Convert Interface to String - Method 2

If you are familar with the fmt.Sprintf function, we can use it to convert an Interface to string as shown in the example below:

func InterfaceToString(i interface{}) string {
    return fmt.Sprintf("%v", i)
}

Example usage:

var iface interface{} = 42
str := InterfaceToString(iface)
fmt.Println(str)

Conclusion

This post covered the basics of interfaces in Go and how to convert a given interface into string representation.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!