Standard library / go/types

go/types.Named.AddMethod()

Adds a method to an uninstantiated named type, if no method with that name already exists. Expected stdout: 'Get\n'.

Formal Syntax & Structure

go syntaxgo
func (t *Named) AddMethod(m *Func)

Parameters & Attributes

NameTypeDescription
m*FuncInput to the operation as described above.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/types")
func main() {  v := types.NewNamed(types.NewTypeName(0, nil, "N", nil), types.Typ[types.Int], nil)  r := types.NewVar(0, nil, "n", v)  f := types.NewFunc(0, nil, "Get", types.NewSignatureType(r, nil, nil, nil, nil, false))  v.AddMethod(f)  fmt.Println(v.Method(0).Name())}
Go Reference