Type Assertions in Go
In Go, a type assertion is a mechanism used to check the actual type of an interface value.
Type assertion is an important tool for handling interface types in Go; it allows us to extract a concrete type from an interface value and operate on it.
Type assertions are commonly used to handle variables of interface type, because an interface variable can store a value of any concrete type that implements the interface.
Basic Syntax
The basic syntax of a type assertion is as follows:
value, ok := interfaceValue.(Type)
interfaceValueis a variable of interface type.Typeis the type you want to assert.valueis the concrete type value after a successful assertion.okis a boolean value indicating whether the assertion succeeded.
If the assertion succeeds,value it will be interfaceValue the actual value ofok is true; if the assertion fails,value it will be Type the zero value ofok is false。
Example
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
"Hello, Go!"
// try to assert i as a string type
s
,
ok
:=
i
.
(
string
)
if
ok
{
fmt
.
Println
(
"assertion succeeded:"
,
s
)
}
else
{
fmt
.
Println
(
"assertion failed"
)
}
// try to assert i as an int type
n
,
ok
:=
i
.
(
int
)
if
ok
{
fmt
.
Println
(
"assertion succeeded:"
,
n
)
}
else
{
fmt
.
Println
(
"assertion failed"
)
}
}
Output result
断言成功: Hello, Go!
断言失败
Another form of type assertion
In addition to the above value, ok := interfaceValue.(Type) form, Go also supports another form of type assertion that does not return a boolean value, but instead directly raises a panic when the assertion fails.
The syntax of this form is as follows:
value := interfaceValue.(Type)
Example Code
Example
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
"Hello, Go!"
// directly assert as the string type
s
:=
i
.
(
string
)
fmt
.
Println
(
"assertion succeeded:"
,
s
)
// directly assert as the int type (this raises a panic)
n
:=
i
.
(
int
)
fmt
.
Println
(
"assertion succeeded:"
,
n
)
}
Output result
断言成功: Hello, Go!
panic: interface conversion: interface {} is string, not int
Common Uses of Type Assertions
1. Handling interface values of multiple types
Go also provides a special type switch syntax to test multiple types:
switch v := i.(type) {
case T1:
// v's type is T1
case T2:
// v's type is T2
default:
// Default case
}
When an interface variable may hold values of multiple types, type assertions can help us perform different operations based on the actual type.
Example
func
printType
(
i
interface
{})
{
switch
v
:=
i
.
(
type
)
{
case
int
:
fmt
.
Println
(
"This is an integer:"
,
v
)
case
string
:
fmt
.
Println
(
"This is a string:"
,
v
)
default
:
fmt
.
Println
(
"Unknown type"
)
}
}
2. Extracting a concrete type from an interface
When working with variables of interface type, we may need to convert them to a concrete type for further operations.
Example
func
processInterface
(
i
interface
{})
{
if
s
,
ok
:=
i
.
(
string
);
ok
{
fmt
.
Println
(
"Handling string:"
,
s
)
}
else
if
n
,
ok
:=
i
.
(
int
);
ok
{
fmt
.
Println
(
"Handling integer:"
,
n
)
}
else
{
fmt
.
Println
(
"Unhandled type"
)
}
}
Notes
- Type Assertions Can Only Be Used on Interface Types: Type assertions can only be used on variables of interface type, not on variables of non-interface types.
- Avoiding Panics: When using a type assertion that does not return a boolean value, always make sure the assertion will not fail, otherwise a panic will be raised.
- Performance of Type Assertions: Type assertions perform type checking at runtime, so they may introduce some performance overhead. Use them with caution in performance-sensitive scenarios.