Standard library / reflect

reflect.Value.MethodByName()

Looks up a bound method by exact name and returns an invalid Value if missing. Expected stdout: '8 false\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) MethodByName(name string) Value

Parameters & Attributes

NameTypeDescription
namestringExact field or method name.

Return Value

Value

Looks up a bound method by exact name and returns an invalid Value if missing.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
type Counter int
func (c Counter) Double() int { return int(c) * 2 }func main() {  v := reflect.ValueOf(Counter(4))  fmt.Println(v.MethodByName("Double").Call(nil)[0].Int(), v.MethodByName("Missing").IsValid())}
Go Reference