[Go] Check whether an interface value satisfies another interface

Created on November 30, 2026

The two-result form of a type assertion, value.(Type), returns an ok flag. Use it to test whether the dynamic value satisfies the target interface before using the result.

package main

type DogInterface interface {
    Bow()
}

type Dog struct{}

func (d *Dog) Bow() {}

type Computer struct{}

func main() {
    var first interface{} = new(Dog)
    if _, ok := first.(DogInterface); !ok {
        panic("first does not satisfy DogInterface")
    }

    var second interface{} = new(Computer)
    if _, ok := second.(DogInterface); !ok {
        panic("second does not satisfy DogInterface")
    }
}

*Dog provides Bow, so the first check succeeds. *Computer does not provide it, so ok is false on the second check. The example explicitly panics and prints this message before the stack trace.

panic: second does not satisfy DogInterface
Go