mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 22:52:05 +00:00
good good study, day day up!
This commit is contained in:
29
vendor/gopl.io/ch12/methods/methods.go
generated
vendored
Normal file
29
vendor/gopl.io/ch12/methods/methods.go
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// 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
|
49
vendor/gopl.io/ch12/methods/methods_test.go
generated
vendored
Normal file
49
vendor/gopl.io/ch12/methods/methods_test.go
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package methods_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopl.io/ch12/methods"
|
||||
)
|
||||
|
||||
func ExamplePrintDuration() {
|
||||
methods.Print(time.Hour)
|
||||
// Output:
|
||||
// type time.Duration
|
||||
// func (time.Duration) Hours() float64
|
||||
// func (time.Duration) Minutes() float64
|
||||
// func (time.Duration) Nanoseconds() int64
|
||||
// func (time.Duration) Seconds() float64
|
||||
// func (time.Duration) String() string
|
||||
}
|
||||
|
||||
func ExamplePrintReplacer() {
|
||||
methods.Print(new(strings.Replacer))
|
||||
// Output:
|
||||
// type *strings.Replacer
|
||||
// func (*strings.Replacer) Replace(string) string
|
||||
// func (*strings.Replacer) WriteString(io.Writer, string) (int, error)
|
||||
}
|
||||
|
||||
/*
|
||||
//!+output
|
||||
methods.Print(time.Hour)
|
||||
// Output:
|
||||
// type time.Duration
|
||||
// func (time.Duration) Hours() float64
|
||||
// func (time.Duration) Minutes() float64
|
||||
// func (time.Duration) Nanoseconds() int64
|
||||
// func (time.Duration) Seconds() float64
|
||||
// func (time.Duration) String() string
|
||||
|
||||
methods.Print(new(strings.Replacer))
|
||||
// Output:
|
||||
// type *strings.Replacer
|
||||
// func (*strings.Replacer) Replace(string) string
|
||||
// func (*strings.Replacer) WriteString(io.Writer, string) (int, error)
|
||||
//!-output
|
||||
*/
|
Reference in New Issue
Block a user