64

I've read "Effective Go" and other Q&As like this: golang interface compliance compile type check , but nonetheless I can't understand properly how to use this technique.

Please, see example:

type Somether interface {
    Method() bool
}

type MyType string

func (mt MyType) Method2() bool {
    return true
}

func main() {
    val := MyType("hello")

    //here I want to get bool if my value implements Somether
    _, ok := val.(Somether)
    //but val must be interface, hm..what if I want explicit type?

    //yes, here is another method:
    var _ Iface = (*MyType)(nil)
    //but it throws compile error
    //it would be great if someone explain the notation above, looks weird
}

Is there any simple ways (eg without using reflection) check value if it implements an interface?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95

7 Answers7

89

You only have to check if a value implements an interface if you don't know the value's type. If the type is known, that check is automatically done by the compiler.

If you really want to check anyways, you can do it with the second method you gave:

var _ Somether = (*MyType)(nil)

which would error at compile time:

prog.go:23: cannot use (*MyType)(nil) (type *MyType) as type Somether in assignment:
    *MyType does not implement Somether (missing Method method)
 [process exited with non-zero status]

What you are doing here, is assigning a pointer of MyType type (and nil value) to a variable of type Somether, but since the variable name is _ it is disregarded.

If MyType implemented Somether, it would compile and do nothing

Dean Elbaz
  • 2,310
  • 17
  • 17
  • Any reason why the black identifier does not necessarily need to be `*Somether`, since the right hand has a *pointer* to `MyType`? I'm still learning. :-) – Victor Zamanian Mar 30 '17 at 23:58
  • 2
    you can think of an interface value like a container, you can put whatever you want in it as long as it implements the right methods. It can contain a pointer to a struct, or a struct directly. As a rule of thumb you never need to make a pointer to an interface value – Dean Elbaz Apr 01 '17 at 05:50
24

Following will work:

val:=MyType("hello")
var i interface{}=val
v, ok:=i.(Somether)
Alpha
  • 668
  • 6
  • 11
17

It is also possible to use Implements(u Type) bool method of reflect.Type in the following way:

package main

import (
    "reflect"
)

type Somether interface {
    Method() bool
}

type MyType string

func (mt MyType) Method() bool {
    return true
}

func main() {

    inter := reflect.TypeOf((*Somether)(nil)).Elem()

    if reflect.TypeOf(MyType("")).Implements(inter) {
        print("implements")
    } else {
        print("doesn't")
    }
}

You can read more on that in the documentation.

dmigo
  • 2,849
  • 4
  • 41
  • 62
13

You can also take Alpha's solution:

val := MyType("hello")
var i interface{} = val
v, ok := i.(Somether)

... and reduce it further:

val := MyType("hello")
v, ok := interface{}(val).(Somether)

If you're trying to test for one-off methods, you can even do something like:

val := MyType("hello")
v, ok := interface{}(val).(interface {
    Method() bool
}) 

NOTE: Make sure you are very careful with "pointer receiver" versus "value receiver" implementations. If the implementation uses a pointer the assertion will fail when passing in a value object. If the implementation uses a value receiver, both assertions will pass.

// Implement Somether as a POINTER receiver method
func (mt *MyType) Method() bool {
  return true
}

func main() {
    val := MyType("hello")

    v, ok := interface{}(val).(Somether)
    fmt.Println(v, ok)
    // Output:  <nil> false

    // Notice the pass by reference
    v, ok := interface{}(&val).(Somether)
    fmt.Println(v, ok)
    // Output:  0xc000010200 true

}

versus

// Implement Somether as a VALUE receiver method
func (mt MyType) Method() bool {
  return true
}

func main() {
    val := MyType("hello")

    v, ok := interface{}(val).(Somether)
    fmt.Println(v, ok)
    // Output:  hello true

    // Notice the pass by reference
    v, ok := interface{}(&val).(Somether)
    fmt.Println(v, ok)
    // Output:  0xc00008e1e0 true

}
Rohan Talip
  • 356
  • 4
  • 13
trevor
  • 151
  • 1
  • 4
3

I have a solution that I use to complement a panic handler pattern
I have not exhaustively tested the code but casual testing is affirmative
I welcome any suggestions to improve or other go expert stuff

// -------------------------------------------------------------- //
// hasErrIface -
// ---------------------------------------------------------------//
func hasErrIface(v reflect.Value) (error, bool) {
    // CanInterface reports whether Interface can be used without panicking
    if !v.CanInterface() {
        return nil, false
    }
    // Interface panics if the Value was obtained by accessing unexported struct fields
    err, ok := v.Interface().(error)
    return err, ok
}

// -------------------------------------------------------------- //
// HasErrKind
// ---------------------------------------------------------------//
func HasErrKind(r interface{}) (err error, isErr bool) {
    err = nil
    isErr = false
    v := reflect.ValueOf(r)
    switch v.Kind() {
    case reflect.Struct:
        errtype := reflect.TypeOf((*error)(nil)).Elem()
        if v.Type().Implements(errtype) {
            err, isErr = v.Interface().(error)
        }
    case reflect.Ptr:
        err, isErr = hasErrIface(v)
    case reflect.Interface:
        err, isErr = hasErrIface(v)
    }
    return
}

// -------------------------------------------------------------- //
// EvalErrKind
// ---------------------------------------------------------------//
func EvalErrKind(r interface{}) (errval error) {
    err, isErr := HasErrKind(r)
    if !isErr {
        errtxt := "Unknown system error - %v :\n%v"
        v := reflect.ValueOf(r)
        return fmt.Errorf(errtxt, v.Type(), v)
    }
    return err
}

// -------------------------------------------------------------- //
// PanicHandler
// ---------------------------------------------------------------//
func PanicHandler(errHandler func(error)) func() {
    return func() {
        var err error
        if r := recover(); r != nil {
            switch r.(type) {
            case ApiError:
                err = r.(ApiError)
            default:
                err = EvalErrKind(r)
            }
            if errHandler != nil {
                errHandler(err)
            }
        }
    }
}
pmg7670
  • 101
  • 5
1

It only works with Go 1.18 or higher

//implements interface
func implInter[T any](obj any) bool {
⠀ _, ok := obj.(T)
  return ok
}

var obj any = "some text"

if impleInter[string](obj) {
⠀ newObj, _ := obj.(string)
} 
0

You can also use this:

If MyType implements the Somether interface, this should compile

// use new to create a pointer to type MyType
var _ Somether = new(MyType) 


var _ Somether = MyType("")
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208