Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "reflect"
  6. )
  7.  
  8. type Interface interface {
  9. Method()
  10. }
  11.  
  12. type Struct struct {
  13. FieldOne FieldOne
  14. }
  15.  
  16. type FieldOne struct {
  17. }
  18.  
  19. func doesFieldImplements(fieldName string) bool {
  20. var s Struct
  21. v := reflect.ValueOf(s)
  22. f := v.FieldByName(fieldName)
  23.  
  24. // how to check here if field is valid and it implements the interface Interface
  25. return false
  26. }
  27.  
  28.  
  29. func main() {
  30. if doesFieldImplements("FieldOne") {
  31. fmt.Println("implements!")
  32. }
  33. }
  34.  
  35. interfaceType := reflect.TypeOf((*Interface)(nil)).Elem()
  36. f.Type().Implements(interfaceType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement