Go type assertion lets you take a value out of an interface and use it as its real, concrete type. In this post, you will use the single value form, the safe “comma ok” form and the type switch. Also, each idea comes with a short example that you can run yourself.
This lesson builds directly on interfaces in Go with examples. You also met map[string]any in Go JSON encoding and decoding. In fact, type assertions are how you read those values.
What Is Go Type Assertion?
An interface value is like a gift box with a label. The label says “any” or “Stringer”, but the real item sits inside. Then a type assertion opens the box and says “I expect a string in here”.
var v any = "hello"
s := v.(string)
fmt.Println(s, len(s)) // hello 5
In short, the syntax is value.(Type). If the guess is right, you get the concrete value. Then you can call len and other string operations on it.
When a Go Type Assertion Fails
var w any = 42
_ = w.(string)
// panic: interface conversion: interface {} is int, not string
A wrong guess with the single value form causes a panic. In other words, a Go type assertion without ok trusts you completely. Therefore, use this form only when you are completely sure about the type.
The Safe comma ok Form
n, ok := v.(int)
fmt.Println(n, ok) // 0 false
if str, ok := v.(string); ok {
fmt.Println("it is a string:", str)
}
// it is a string: hello
However, with two results, a wrong guess never panics. Instead, ok becomes false, and the value is the zero value of the type. Indeed, this is the same “comma ok” idea you used with maps in Go.
Type Switches: Checking Many Types
When a value could be one of several types, a type switch is cleaner than a chain of assertions. For example, the diagram below shows one value flowing into the matching case.

A type switch checks the dynamic type inside the interface and runs the first case that matches.
func describe(v any) string {
switch x := v.(type) {
case int:
return "int doubled: " + strconv.Itoa(x*2)
case string:
return "string of length " + strconv.Itoa(len(x))
case bool:
return fmt.Sprint("bool flipped: ", !x)
case nil:
return "nil value"
default:
return fmt.Sprintf("other type %T", x)
}
}
fmt.Println(describe(21)) // int doubled: 42
fmt.Println(describe("Go")) // string of length 2
fmt.Println(describe(true)) // bool flipped: false
fmt.Println(describe(nil)) // nil value
fmt.Println(describe(3.14)) // other type float64
Inside each case, x already has the matching type, so no extra Go type assertion is needed. So in the int case you can multiply it, and in the string case you can take its length.
Go Type Assertion to an Interface
type Celsius float64
func (c Celsius) String() string {
return strconv.FormatFloat(float64(c), 'f', 1, 64) + "°C"
}
var t any = Celsius(20)
if st, ok := t.(fmt.Stringer); ok {
fmt.Println(st.String()) // 20.0°C
}
You can also assert to another interface. In that case, the check asks whether the value has those methods. For example, this pattern lets a function add extra features when a value supports them.
Type Assertion vs Type Conversion
| Feature | Type assertion | Type conversion |
|---|---|---|
| Syntax | v.(string) | float64(n) |
| Works on | Interface values only | Concrete values |
| Checked at | Run time | Compile time |
| Can panic | Yes, single value form | No |
In short, a conversion changes a value from one type to another. Also, it works on any compatible types and never fails at run time. An assertion, however, only reveals the type that is already inside an interface.
Common Mistakes with Go Type Assertion
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| panic: interface conversion: interface {} is int, not string | Because the guess was wrong. | So use the comma ok form. |
| invalid operation: x (variable of type int) is not an interface | Because the value is not an interface. | So use a conversion instead. |
| JSON number fails as int | Because decoded numbers are float64. | So assert to float64 first. |
Also, when you check errors, prefer errors.As over a raw assertion, because it looks through wrapped errors too. Interface questions are common in interviews, so practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
Quick Recap
- Go type assertion reads the concrete value inside an interface.
- The single value form panics on a wrong guess.
- The comma ok form returns false instead of panicking.
- A type switch handles many possible types cleanly.
- You can also assert to another interface, such as fmt.Stringer.
What’s Next?
Finally, you can look inside any interface. Next, we will reuse code by placing one struct inside another with struct embedding.
Next lesson: Go Struct Embedding Explained with Examples


