1// Copyright 2017, The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
5// Package function provides functionality for identifying function types.
20 tbFunc // func(T) bool
21 ttbFunc // func(T, T) bool
22 ttiFunc // func(T, T) int
23 trbFunc // func(T, R) bool
24 tibFunc // func(T, I) bool
27 Equal = ttbFunc // func(T, T) bool
28 EqualAssignable = tibFunc // func(T, I) bool; encapsulates func(T, T) bool
29 Transformer = trFunc // func(T) R
30 ValueFilter = ttbFunc // func(T, T) bool
31 Less = ttbFunc // func(T, T) bool
32 Compare = ttiFunc // func(T, T) int
33 ValuePredicate = tbFunc // func(T) bool
34 KeyValuePredicate = trbFunc // func(T, R) bool
37var boolType = reflect.TypeOf(true)
38var intType = reflect.TypeOf(0)
40// IsType reports whether the reflect.Type is of the specified function type.
41func IsType(t reflect.Type, ft funcType) bool {
42 if t == nil || t.Kind() != reflect.Func || t.IsVariadic() {
45 ni, no := t.NumIn(), t.NumOut()
47 case tbFunc: // func(T) bool
48 if ni == 1 && no == 1 && t.Out(0) == boolType {
51 case ttbFunc: // func(T, T) bool
52 if ni == 2 && no == 1 && t.In(0) == t.In(1) && t.Out(0) == boolType {
55 case ttiFunc: // func(T, T) int
56 if ni == 2 && no == 1 && t.In(0) == t.In(1) && t.Out(0) == intType {
59 case trbFunc: // func(T, R) bool
60 if ni == 2 && no == 1 && t.Out(0) == boolType {
63 case tibFunc: // func(T, I) bool
64 if ni == 2 && no == 1 && t.In(0).AssignableTo(t.In(1)) && t.Out(0) == boolType {
67 case trFunc: // func(T) R
68 if ni == 1 && no == 1 {
75var lastIdentRx = regexp.MustCompile(`[_\p{L}][_\p{L}\p{N}]*$`)
77// NameOf returns the name of the function value.
78func NameOf(v reflect.Value) string {
79 fnc := runtime.FuncForPC(v.Pointer())
83 fullName := fnc.Name() // e.g., "long/path/name/mypkg.(*MyType).(long/path/name/mypkg.myMethod)-fm"
85 // Method closures have a "-fm" suffix.
86 fullName = strings.TrimSuffix(fullName, "-fm")
89 for len(fullName) > 0 {
90 inParen := strings.HasSuffix(fullName, ")")
91 fullName = strings.TrimSuffix(fullName, ")")
93 s := lastIdentRx.FindString(fullName)
98 fullName = strings.TrimSuffix(fullName, s)
100 if i := strings.LastIndexByte(fullName, '('); inParen && i >= 0 {
101 fullName = fullName[:i]
103 fullName = strings.TrimSuffix(fullName, ".")
105 return strings.TrimSuffix(name, ".")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list