SoFunction
Updated on 2025-05-23

The specific use of Golang interface{}

1. What is interface{}?

In Go,interface{}It's a kindempty interface, it meansAny type. Because it does not define any method,All types implement it

Definition form:

interface{}

Equivalent to:

type interface{} interface {}

2. What's special about interface{}?

✅ Features:

  • All types are implementedinterface{}

  • Can be usedStore values ​​of any type

  • It's GoUniversal type container, similar to those in other languagesObjectorany

3. Use examples

1. Store values ​​of any type:

func printAnything(v interface{}) {
    ("Value is:", v)
}
​
func main() {
    printAnything(123)
    printAnything("hello")
    printAnything([]int{1, 2, 3})
}

Output:

Value is: 123
Value is: hello
Values ​​are: [1 2 3]

4. The underlying principle: How does interface{} save the value?

The Go compiler willinterface{}The actual storage is two parts:

type eface struct {
    _type *_type      // Real type information    data    // Pointer to actual data}

for example:

var a interface{} = 123

At this time:

  • _type: Point tointtype descriptor;

  • data: Point to the value of 123, the int.

5. How to get out the value in interface{}?

1. Type Assertion:

var a interface{} = "hello"
​
str, ok := a.(string)
if ok {
    ("Successful conversion:", str)
} else {
    ("Conversion failed")
}

2. Use Type Switch:

var a interface{} = 3.14
​
switch v := a.(type) {
case int:
    ("Yes int:", v)
case float64:
    ("Yes float64:", v)
case string:
    ("Yes string:", v)
default:
    ("Unknown Type")
}

6. Common usage scenarios

Scene describe
JSON parsing map[string]interface{}Can store any structure
What is received is...interface{}Parameters
Transfer parameters of any type Write a general tool function that allows receiving any type
Null value or unknown type variable When you don't know the variable type, save asinterface{}

7. Things to note

  • interface{}What type of deposited and when withdrawnMust assert correct, otherwise an error will be reported during runtime.

  • interface{}You cannot directly do addition, subtraction, multiplication and division operations, and you must first assert the type.

  • Not equal to JavaScriptany,Go is still a strongly typed language, and type assertions are important.

  • interface{}Cannot compare directly unless internal types support==

8. Frequently Asked Questions and Answers in Interview

Q: Is interface{} omnipotent?

A: It can store any type of value, but you can't manipulate these values ​​at will unless you know its true type and use type assertions to restore it.

Q: What is the difference between interface{} and interface?

A:interface{}It is an interface type,No method is defined;And ordinary interfaces such asWriter interface { Write(p []byte) (n int, err error) }Methods are defined, and only the types that implement these methods can be assigned to the interface.

9. A summary sentence:

interface{}It is an "empty interface" that can represent any type in Go, and is one of the basic tools for implementing generic programming.

This is the introduction to this article about the specific use of Golang interface{}. For more related Golang interface{}, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!