Source file test/reflectmethod6.go

     1  // run
     2  
     3  // Copyright 2020 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Similar to reflectmethod5.go, but for reflect.Type.MethodByName.
     8  
     9  package main
    10  
    11  import "reflect"
    12  
    13  var called bool
    14  
    15  type foo struct{}
    16  
    17  func (foo) X() { called = true }
    18  
    19  var h = reflect.Type.MethodByName
    20  
    21  func main() {
    22  	v := reflect.ValueOf(foo{})
    23  	m, ok := h(v.Type(), "X")
    24  	if !ok {
    25  		panic("FAIL")
    26  	}
    27  	f := m.Func.Interface().(func(foo))
    28  	f(foo{})
    29  	if !called {
    30  		panic("FAIL")
    31  	}
    32  }
    33  

View as plain text