Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "reflect"
- )
- type Interface interface {
- Method()
- }
- type Struct struct {
- FieldOne FieldOne
- }
- type FieldOne struct {
- }
- func doesFieldImplements(fieldName string) bool {
- var s Struct
- v := reflect.ValueOf(s)
- f := v.FieldByName(fieldName)
- // how to check here if field is valid and it implements the interface Interface
- return false
- }
- func main() {
- if doesFieldImplements("FieldOne") {
- fmt.Println("implements!")
- }
- }
- interfaceType := reflect.TypeOf((*Interface)(nil)).Elem()
- f.Type().Implements(interfaceType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement