mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-14 02:13:51 +00:00
30 lines
621 B
Go
30 lines
621 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 351.
|
|
|
|
// Package methods provides a function to print the methods of any value.
|
|
package methods
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
//!+print
|
|
// Print prints the method set of the value x.
|
|
func Print(x interface{}) {
|
|
v := reflect.ValueOf(x)
|
|
t := v.Type()
|
|
fmt.Printf("type %s\n", t)
|
|
|
|
for i := 0; i < v.NumMethod(); i++ {
|
|
methType := v.Method(i).Type()
|
|
fmt.Printf("func (%s) %s%s\n", t, t.Method(i).Name,
|
|
strings.TrimPrefix(methType.String(), "func"))
|
|
}
|
|
}
|
|
|
|
//!-print
|