Mastering Go’s Empty Interface: Store Any Type and Copy Data Safely
This guide explains Go's empty interface, how any value can be stored in it, demonstrates declaring variables and functions with interface{}, shows practical slice examples, discusses memory layout, and provides the correct way to copy data into an empty‑interface slice.
What Is an Empty Interface?
An empty interface in Go is declared without any methods, meaning every type satisfies it and can be assigned to a variable of this type. type empty_int interface{} It is usually written as interface{} and used directly.
Declaring and Using Empty Interface Variables
You can create a variable of the empty interface type: var i interface{} Functions can also accept parameters of this type, allowing any argument to be passed: func myfunc(i interface{}) { /* ... */ } Many standard library functions, such as fmt.Println, use ...interface{} to accept arbitrary arguments.
Storing Arbitrary Values in Collections
Because any type implements the empty interface, you can build slices, maps, or structs that hold values of any type.
package main
import "fmt"
func main() {
any := make([]interface{}, 5)
any[0] = 11
any[1] = "hello world"
any[2] = []int{11, 22, 33, 44}
for _, v := range any {
fmt.Println(v)
}
}The output shows integers, strings, slices, and nil entries, illustrating Go’s ability to store heterogeneous data.
Embedding Empty Interface in Structs
type my_struct struct {
anything interface{}
anythings []interface{}
}This lets struct fields hold values of any type.
Copying Data into an Empty‑Interface Slice
Directly assigning a slice of a concrete type to a slice of interface{} fails because the memory layout differs: each empty‑interface element occupies two machine words (type pointer and data pointer), while a slice of int stores raw integers.
package main
import "fmt"
func main() {
testSlice := []int{11, 22, 33, 44}
var newSlice []int
newSlice = testSlice // works
fmt.Println(newSlice)
var any []interface{}
any = testSlice // compile error
fmt.Println(any)
}To copy correctly, iterate over the source slice and append each element to the empty‑interface slice:
var any []interface{}
for _, v := range testSlice {
any = append(any, v)
}This creates a slice where each element is an independent empty‑interface instance pointing to the original data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
