Menu
Coddy logo textTech

Empty Interface

Part of the Logic & Flow section of Coddy's GO journey — lesson 16 of 68.

So far you've worked with interfaces that require specific methods, but Go has a special interface that's incredibly flexible: the empty interface, written as interface{}.

The empty interface has no method requirements at all, which means any type in Go automatically satisfies it. Since every type implements zero methods (at minimum), every type can be assigned to an interface{} variable:

var anything interface{}
anything = 42        // int
anything = "hello"   // string
anything = true      // bool
anything = []int{1, 2, 3}  // slice

This makes interface{} extremely useful when you need to work with data of unknown or varying types. You might encounter this when reading configuration files, handling user input, or working with APIs that return different data types depending on the situation.

However, since the empty interface can hold any type, you'll often need to determine what type is actually stored inside it before you can use the value meaningfully. This is where type assertions and type switches (which you'll learn about next) become essential tools for working safely with interface{} values.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

In this challenge, you'll practice working with the empty interface by creating a data processor that can handle different types of values. You'll create a function that accepts an interface{} parameter and processes various data types.

You will receive two inputs:

  • A string representing the data type (e.g., "int", "string", "bool", or "slice")
  • A string representing the actual value (e.g., "42", "hello", "true", or "1,2,3")

Your task is to:

  1. Create a function called processData that takes an interface{} parameter and prints information about the value
  2. Based on the data type input, convert the value string to the appropriate Go type:
    • For "int": convert to integer
    • For "string": use as string
    • For "bool": convert to boolean
    • For "slice": convert comma-separated values to a slice of integers
  3. Call the processData function with the converted value
  4. Inside processData, print the value and its type in the format: "Value: [value], Type: [type]"

For the slice input, the comma-separated string like "1,2,3" should be converted to []int{1, 2, 3}. The function should work with any type since it accepts interface{}, demonstrating how the empty interface can hold values of any type. Use fmt.Printf with the %v and %T format verbs to display the value and type respectively.

Cheat sheet

The empty interface interface{} has no method requirements, meaning any type in Go automatically satisfies it:

var anything interface{}
anything = 42        // int
anything = "hello"   // string
anything = true      // bool
anything = []int{1, 2, 3}  // slice

The empty interface is useful for working with data of unknown or varying types, such as configuration files, user input, or APIs that return different data types.

Use fmt.Printf with %v and %T format verbs to display the value and type of an interface{} variable:

fmt.Printf("Value: %v, Type: %T", value, value)

Useful conversion helpers from the standard library:

import (
    "strconv"
    "strings"
)

// Convert string to int
n, err := strconv.Atoi("42")       // n = 42

// Convert string to bool
b, err := strconv.ParseBool("true") // b = true

// Split a string into a slice
parts := strings.Split("a,b,c", ",") // ["a", "b", "c"]

Try it yourself

package main

import (
	"fmt"
)

func main() {
	// Read input
	var dataType string
	var valueStr string
	fmt.Scanln(&dataType)
	fmt.Scanln(&valueStr)
	
	// TODO: Write your code below
	// 1. Create the processData function that accepts interface{}
	// 2. Convert valueStr to the appropriate type based on dataType
	// 3. Call processData with the converted value
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow