How to Manage Functions for Similar Go Structs with Shared Fields?

DDD
Release: 2024-10-29 04:52:02
Original
167 people have browsed it

How to Manage Functions for Similar Go Structs with Shared Fields?

Go Best Practice: Managing Functions for Similar Structs with Shared Fields

In Go, it's common to encounter multiple structs with similar fields, and there is a need to perform identical operations on them. To avoid code repetition while maintaining flexibility, consider the following strategies:

Creating a Custom Type for the Shared Field:

If the shared field is a simple data type (e.g., string), consider defining a custom type for it. This allows you to attach methods to the custom type, which can then be used by any struct that embeds that type.

<code class="go">type Version string

func (v Version) PrintVersion() {
    fmt.Println("Version is", v)
}</code>
Copy after login

Then, embed the Version type in the structs:

<code class="go">type Game struct {
    Name               string
    MultiplayerSupport bool
    Genre              string
    Version
}

type ERP struct {
    Name               string
    MRPSupport         bool
    SupportedDatabases []string
    Version
}</code>
Copy after login

This allows you to print the version using the PrintVersion method on the Version field:

<code class="go">g.PrintVersion()
e.PrintVersion()</code>
Copy after login

Using Reflection:

If the shared field can be different types or if you want more flexibility, you can use reflection to dynamically invoke the appropriate method. This approach is more complex and has some performance implications, but it provides greater flexibility.

<code class="go">type Printer interface {
    PrintVersion() error
}

func PrintVersion(p Printer) error {
    t := reflect.TypeOf(p)
    method, ok := t.MethodByName("PrintVersion")
    if !ok {
        return fmt.Errorf("object doesn't have a PrintVersion method")
    }

    return method.Func.Call([]reflect.Value{reflect.ValueOf(p)})[0].Interface().(error)
}</code>
Copy after login

You can then use the PrintVersion function to invoke the PrintVersion method on any object that implements the Printer interface:

<code class="go">var game Game
var erp ERP

PrintVersion(game)
PrintVersion(erp)</code>
Copy after login

The above is the detailed content of How to Manage Functions for Similar Go Structs with Shared Fields?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!