To create a data structure that can contain any type in Go, consider utilizing the empty interface.
According to the Go Programming Language Specification, "all types implement the empty interface." This means that you can define a variable of type interface{} and assign it a value of any non-interface type.
var value interface{} = 10
This functionality allows you to create data structures that can store values of any type, similar to void pointers in C. For example, you could define a linked list where each node can contain a value of any type:
type Node struct { Value interface{} Next *Node }
Starting in Go 1.18, the builtin alias any was introduced as a synonym for interface{}. It provides a more concise and descriptive name for the empty interface.
var value any = 10
By utilizing the empty interface or its alias any, you can create Go data structures capable of storing values of any type, achieving functionality similar to void pointers in C.
The above is the detailed content of What is Go's Equivalent to C's Void Pointers?. For more information, please follow other related articles on the PHP Chinese website!