Sling Academy
Home/Golang/Defining and Using Empty Interfaces (`interface{}`) in Go

Defining and Using Empty Interfaces (`interface{}`) in Go

Last updated: November 26, 2024

In Go, empty interfaces are a powerful feature that allows developers to write more flexible and generic code. The empty interface is declared using interface{} and can hold values of any type because every type in Go implements the empty interface by default. This article will walk you through understanding, defining, and using empty interfaces in Go, with examples ranging from basic to advanced applications.

Basic Usage of Empty Interface

Let's start with the simplest way to use an empty interface. Imagine that you want to create a function that accepts any type of parameter. This is where empty interfaces come in handy. Here's a basic example:

package main

import "fmt"

func printValue(v interface{}) {
    fmt.Println(v)
}

func main() {
    printValue(42)
    printValue("Hello")
    printValue(3.14)
}

In this example, the function printValue takes a parameter of type interface{}, allowing it to accept any type of argument. You can see it successfully prints an integer, a string, and a float.

Intermediate Level: Type Assertion

Once a value is stored in an empty interface variable, you often need to know its actual type to use it effectively. Go provides a way to achieve this using type assertion. Here is how you can use it:

package main

import "fmt"

func describe(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("%v is an int\n", v)
    case string:
        fmt.Printf("%v is a string\n", v)
    default:
        fmt.Printf("Unknown type %T\n", v)
    }
}

func main() {
    describe(42)
    describe("A string")
    describe(3.14)
}

Here, the describe function uses a type switch, a special type of switch statement for discriminating types. It safely discovers the dynamic type of the interface{} value at runtime.

Advanced Usage: Flexible Collections

One of the advanced uses of empty interfaces in Go is to create flexible data structures, such as collections that can store various types of items. Here's an example:

package main

import "fmt"

type AnySlice []interface{}

func main() {
    var data AnySlice = []interface{}{123, "Go", 9.58}

    for index, value := range data {
        switch v := value.(type) {
        case int:
            fmt.Printf("Element [%d] is an int: %d\n", index, v)
        case string:
            fmt.Printf("Element [%d] is a string: %s\n", index, v)
        case float64:
            fmt.Printf("Element [%d] is a float64: %f\n", index, v)
        default:
            fmt.Printf("Element [%d] is of an unknown type\n", index)
        }
    }
}

In this example, we create a slice of interface{} and populate it with different types. We then iterate over the collection, determining each element's type using type assertion.

Conclusion

Empty interfaces in Go provide great flexibility and are particularly useful in scenarios where you need a method to handle data of various kinds. However, using them comes with a trade-off, mainly reduced type safety and the potential need for type assertions. By understanding and using empty interfaces wisely, you can write versatile and powerful Go programs.

Next Article: Type Assertions and Type Switches with Interfaces in Go

Previous Article: How Interfaces Work in Go: Implicit Implementation Explained

Series: Structs and Interfaces in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant