Tips To Convert String to Interface in Golang

Go is an open-source, statically typed, compiled, and general-purpose programming language to develop reliable and efficient software simply. Go is, also known as Golang (Go language), created by Google engineers. It takes simplicity and productivity like Python along with having C ability. Programmers and developers use it as it offers automatic garbage collection, portability, high-speed executive, quick compilation, memory safety, independent error handling, and built-in libraries. Go is ideally used for various applications like cloud applications, DevOps, server-side applications as well as command-line tools. It is really easy to learn and simple to work with, and that’s why beginners are learning it as it is also easy to read. There are many projects you can try on Golang to learn it. Today, we discuss tips to convert string to interface in golang.

Before starting to covert, you need to understand what string and interface are in Golang. Let’s have a look at it.

String in Golang

A string in Goland is a slice of bytes, which is not similar to the string used in languages like C++, Java, and Python. It is also defined as a read-only slice of bytes, and bytes are represented using UTF-8 encoding. In Go, strings can be denoted with double quotes (“ “). Have a look at the example to create a string

package main

import (  
    "fmt"
)

func main() {  
    name := "Welcome to Tutopal"
    fmt.Println(name)
}

Output

Welcome to Tutopal

Interface in Golang

Interface in Golang is a set of method signatures, which is not similar to the one used in different languages. It is a custom type to be used to define a set of one or more method signatures. The interface is also defined as the collection of methods, which is a custom type.

Syntax

type interface_name interface{

// Method signatures

}

Have a look at the example

type Shape interface {
  area() float32
  perimeter() float32
}

Here, the interface is ‘shape’ with methods area() and perimeter(). It is evidently seen that with no implementation, methods only have method signatures.

How To Convert String to Interface in Golang

To convert, you need to create the utility function first. Create it like this:

func ToGenericArray(arr ...interface{}) []interface{} {
    return arr
}

Once you create it, use the following to convert

func yourfunc(arr []interface{})  {
....
}

yourfunc(ToGenericArray([...]string{"a", "b", "c"}))

You need to avoid using the following as it is never going to work

func yourfunc(arr []interface{})  {
....
}

arr:=[...]string{"a", "b", "c"}

yourfunc(ToGenericArray(arr))

Examples To Convert String to Interface

We have come up with two amazing examples that you can use to convert. Have look at those

Example 1

iAreaId := val.(int)
iAreaId, ok := val.(int) // Alt. non panicking version 

Example 2

var myInt interface{}
myInt = 8

toInt, ok := myInt.(int)
fmt.Println(toInt, ok) // 8 true

toString, ok := myInt.(string)
fmt.Println(toString, ok) // "" false

You can simply use the examples to convert string to the interface.

Conclusion

In this post, we discussed important tips to help you convert string to interface in Golang. You can refer to the post whenever you want to convert.

I hope you enjoyed it and find it helpful!

I wish you all the best!

Leave a Reply

Your email address will not be published. Required fields are marked *