Unlock Go’s Empty Interface: Store Any Type Anywhere
An empty interface in Go, defined without methods, can hold values of any type, enabling flexible data structures like slices, maps, and structs, and is commonly used in functions such as fmt.Println; this article explains its definition, usage, memory layout, and copying nuances with code examples.
Empty Interface
An empty interface is an interface that defines no methods. Because there are no methods to implement, any Go value satisfies the empty interface, allowing any object to be stored in a variable of this type.
Definition:
type empty_int interface {}It is often abbreviated as:
type empty_int interface{}More commonly, the built‑in type interface{} is used directly, for example:
var i interface{}Functions can also accept an empty‑interface parameter:
func myfunc(i interface{})The fmt package’s printing functions use empty interfaces for their variadic arguments:
func Println(a ...interface{}) (n int, err error)Empty Interface Data Structures
You can define arrays, slices, maps, or structs whose element or field type is interface{}, allowing them to store values of any type.
Example: creating a slice of empty interfaces:
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 _, value := range any {
fmt.Println(value)
}
}Output:
11
hello world
[11 22 33 44]
<nil>
<nil>This demonstrates that, like dynamic languages, Go can store heterogeneous data in a single collection using the empty interface.
Structs can also contain fields of type interface{} to hold arbitrary values:
type my_struct struct {
anything interface{}
anythings []interface{}
}Copying Data Structures to an Empty‑Interface Slice
Assigning a value of any type to an empty‑interface variable stores two pointers: one to the type information and one to the actual value.
Because each empty‑interface value occupies two machine words, a slice of empty interfaces uses N*2 machine‑word slots for N elements, which differs from a slice of concrete types.
Directly assigning a slice of a concrete type to a slice of empty interfaces causes a compile‑time error. Instead, copy element by element:
package main
import "fmt"
func main() {
testSlice := []int{11, 22, 33, 44}
// Successful copy of concrete slice
var newSlice []int
newSlice = testSlice
fmt.Println(newSlice)
// Failed copy to empty‑interface slice (compile error)
// var any []interface{}
// any = testSlice // cannot assign []int to []interface{}
// Correct way: copy each element
var any []interface{}
for _, value := range testSlice {
any = append(any, value)
}
fmt.Println(any)
}Using a loop (or other conversion) ensures each element is stored as an individual empty‑interface value, preserving the underlying data correctly. The same approach applies to other data structures that contain empty‑interface fields.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
