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

Generic structure that accepts any structure

王林
Release: 2024-02-06 08:21:04
forward
858 people have browsed it

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)
}
Copy after login

main content

NewUser()
Copy after login

mistake

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


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)
}
Copy after login

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>
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template