Pular para o conteúdo principal
eLearner.app
Módulo 6 · Lição 2 de 527/50 no curso~10 min
Lições do módulo (2/5)

Afirmação de tipo

A type assertion extracts the concrete type from an interface value. You need it when you have an interface{} (or a "high" interface) in hand and need to treat it as its specific type to access fields or methods not present on the interface.

Syntax

Go
var i interface{} = "ciao"

s := i.(string)           // "panic" form: panics if it's not a string
s, ok := i.(string)       // "comma ok" form: ok = false if it's not a string

Two forms, two semantics:

FormSuccessFailure
v := i.(T)v = value of type Tpanic at runtime
v, ok := i.(T)v=..., ok=truev = zero value of T, ok=false

"Comma ok" form: the safest

Go
var i interface{} = 42

if s, ok := i.(string); ok {
    fmt.Println("è stringa:", s)
} else {
    fmt.Println("non è stringa, ignoro")
}

ok lets you handle the "wrong type" case without a panic. It's the idiomatic pattern in the vast majority of cases.

Assertion to an interface

The assertion doesn't work only on concrete types, but also on other interfaces:

Go
type Closer interface { Close() error }

func tryClose(x interface{}) {
    if c, ok := x.(Closer); ok {
        c.Close()
    }
}

A very common pattern in the standard library: "if my input is also an io.Closer, call Close at the end".

Assertion on pointers vs values

The assertion must match the dynamic type EXACTLY:

Go
type Cat struct{}
func (c *Cat) Meow() {}

var a interface{} = &Cat{}

c1, ok := a.(*Cat)   // ok = true
c2, ok := a.(Cat)    // ok = false: it holds *Cat, not Cat

*Cat and Cat are distinct types for the type system.

When to use assertion vs type switch

  • Only 1 possible type, "feature-detect" check (e.g. cast to an optional interface): type assertion with comma ok.
  • 2+ possible types, dispatch over multiple branches: type switch (next lesson).

Try it

Exercício#go.m6.l2.e1
Tentativas: 0Carregando…

Use the comma ok pattern to extract a string from i.

Carregando editor…
Mostrar dica

Syntax: `v, ok := i.(T)`.

Solução disponível após 3 tentativas

Exercício#go.m6.l2.e2
Tentativas: 0Carregando…

Type assertion to int without comma ok (assume i contains an int).

Carregando editor…
Mostrar dica

Without `ok` you assume the type is certain; otherwise panic.

Solução disponível após 3 tentativas

Quiz#go.m6.l2.e3
Pronto

What happens with `s := i.(string)` if i contains an int?

Go
var i interface{} = 42
s := i.(string)
Opções de resposta

Recap

  • v := i.(T): panic if the dynamic type ≠ T.
  • v, ok := i.(T): safe pattern, check ok.
  • Works also against another interface ("feature detect").
  • T and *T are distinct types for the assertion.
  • 1 type → assertion; 2+ types → type switch.