Home >Backend Development >Golang >How to Manage Functions for Similar Go Structs with Shared Fields?

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

DDD
DDDOriginal
2024-10-29 04:52:02260browse

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>

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>

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

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

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>

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>

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!

Statement:
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