Home  >  Article  >  Backend Development  >  Generic structure that accepts any structure

Generic structure that accepts any structure

王林
王林forward
2024-02-06 08:21:04837browse

Generic structure that accepts any structure

Question content

How to make a universal structure that accepts any structure?

package model

type model struct {
    m *interface{}
}

func (m *model) Select(){
    
}

type (
    field struct {
        table   string
        field   string
    }
    fields  map[string]field
)

type User struct {
    schema      string
    fields      fields
}

func NewUser() *interface{} {
    model_user := &User{
        schema: "main",
        
        fields: fields{
            "id":           field{"user","id"},
            "client_id":    field{"user","client_id"},
            "email":        field{"user","email"},
        },
    }
    return model(model_user)
}

main content

NewUser()

mistake

cannot convert model_user (variable of type *User) to type model

Correct answer


By definition, the model structure appears to exist for adding the Select() function to ( Or try adding) to the values ​​contained in the model.

i.e. you seem to want some type that provides the ability to call Select() and perform some operation on any arbitrary type of value (presumably in the Select() implementation Use some form of type switch).

If so, then you're better off using the interface directly and eliminating the model middleman:

type Selectable interface {
  Select()
}

type User struct {
  //...
}

func (u *User) Select() {
   // implement Select as appropriate for the User type
}

func NewUser() *User {
  return &User{
     // ...
  }
}

func Select(s Selectable) {
   s.Select()
}

func main() {
   u := NewUser()
   Select(u)
}

You will find that the Select(Selectable) function is redundant (you can call u.Select() directly; it is provided only to illustrate that when Selectable## is needed # Any type of value can be used, provided that the type implements the Selectable interface.

interfaces in GoLang provides duck typing - if a type implements the contract of an interface, then it implements that interface, even if the concrete type does not know any formal interface definition beforehand. i.e. “If it looks like a duck and quacks like a duck, then it is a duck”. If the goal is to remove the

Select()ing logic from the User type (or other type) and isolate it in a separate "selector", then yes Implement this again by removing the model intermediary and simply implementing a func that performs the type conversion:

<code>func Select(v any) error {
   switch v := v.(type) {
      case *User:
         // ... implement for *User or call some private fn which encapsulates this behaviour
      default:
          return errors.New("value cannot be Select()ed")
    }
}
</code>

The above is the detailed content of Generic structure that accepts any structure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete