Created
January 14, 2023 10:21
-
-
Save podhmo/0c694c9c3e5a5ec785b3e053dd12823b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
func Foo(int) {} | |
func Bar(int) {} | |
type S struct{} | |
func (s *S) M(int) {} | |
func main() { | |
{ | |
rt := reflect.TypeOf(Foo) | |
fmt.Println(rt, rt.Kind()) | |
rt2 := reflect.TypeOf(Bar) | |
fmt.Println(rt2, rt2.Kind()) | |
rt3 := reflect.TypeOf(func(int) {}) | |
fmt.Println(rt3, rt3.Kind()) | |
rt4 := reflect.TypeOf(func(int) {}) | |
fmt.Println(rt4, rt4.Kind()) | |
rt5 := reflect.TypeOf(new(S).M) | |
fmt.Println(rt5, rt5.Kind()) | |
fmt.Println("rt1 == rt2", rt == rt2) // true | |
fmt.Println("rt1 == rt3", rt == rt3) // true | |
fmt.Println("rt1 == rt4", rt == rt4) // true | |
fmt.Println("rt1 == rt5", rt == rt5) // true | |
fmt.Println("rt3 == rt4", rt3 == rt4) // true | |
} | |
{ | |
x := id(Foo) | |
fmt.Println(x) | |
x2 := id(Bar) | |
fmt.Println(x2) | |
x3 := id(func(int) {}) | |
fmt.Println(x3) | |
x4 := id(func(int) {}) | |
fmt.Println(x4) | |
x5 := id(new(S).M) | |
fmt.Println(x5) | |
x6 := id(Foo) | |
fmt.Println(x6) | |
x7 := id(new(S).M) | |
fmt.Println(x7) | |
fmt.Println("x1 == x2", x == x2) // false | |
fmt.Println("x1 == x3", x == x3) // false | |
fmt.Println("x1 == x4", x == x4) // false | |
fmt.Println("x1 == x5", x == x5) // false | |
fmt.Println("x1 == x6", x == x6) // true | |
fmt.Println("x1 == x7", x == x7) // false | |
fmt.Println("x3 == x4", x3 == x4) // false | |
fmt.Println("x5 == x7", x5 == x7) // true | |
} | |
{ | |
type Foo struct{ Name string } | |
type Bar struct{ Name string } | |
x := id(Foo{}) | |
fmt.Println(x) | |
x2 := id(Bar{}) | |
fmt.Println(x2) | |
x3 := id(struct{ Name string }{}) | |
fmt.Println(x3) | |
x4 := id(struct{ Name string }{}) | |
fmt.Println(x3) | |
x5 := id(Foo{}) | |
fmt.Println(x5) | |
fmt.Println("x1 == x2", x == x2) // false | |
fmt.Println("x1 == x3", x == x3) // false | |
fmt.Println("x1 == x4", x == x4) // false | |
fmt.Println("x1 == x5", x == x5) // true | |
fmt.Println("x3 == x4", x3 == x4) // true | |
} | |
} | |
func id(ob any) ID { | |
// see: reflect.TypeOf() | |
x := ID{Type: reflect.TypeOf(ob)} | |
if x.Type.Kind() == reflect.Func { | |
x.pc = reflect.ValueOf(ob).Pointer() | |
} | |
return x | |
} | |
type ID struct { | |
Type reflect.Type | |
pc uintptr | |
} | |
func (id *ID) Kind() reflect.Kind { | |
return id.Type.Kind() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment