如何为嵌套结构中的多个字段设置默认值

huangapple go评论104阅读模式
英文:

How to set default values for multiple fields in a nested struct

问题

假设你有以下代码:

type Animal struct {
	Name string
	Ears int
	Eyes int
}

type Dog struct {
	Animal `default:"Animal{Ears:'2', Eyes:'2'}"`
}

有没有一种方法可以设置默认值?上面的代码无法正常工作。

  1. default:{Ears:'2', Eyes:'2'}
  2. default.Ears:"2", default.Eyes:"2"}
英文:

Suppose you have the following:

type Animal struct {
	Name string
	Ears int
	Eyes int
}

type Dog struct {
	Animal `default:"Animal{Ears:'2', Eyes:'2'}"`
}

Is there a way to set the default? It doesn't work in the above.

  1. default:{Ears:'2', Eyes:'2'}
  2. default.Ears:"2", default.Eyes:"2"}

答案1

得分: 1

你尝试使用的语法不受支持。你需要使用构造函数。请参考https://www.geeksforgeeks.org/how-to-assign-default-value-for-struct-field-in-golang/,了解一种可行的方法。

英文:

The syntax you're trying to use is not supported. You have to use a constructor function. See https://www.geeksforgeeks.org/how-to-assign-default-value-for-struct-field-in-golang/ for one decent discussion of that approach.

答案2

得分: 1

在提供的代码中,struct Dog 嵌入了 Animal struct,并且似乎试图为嵌入的 Animal struct 的 Ears 和 Eyes 字段设置默认值。然而,使用的语法对于设置默认值是无效的。

在 Go 语言中,你不能直接使用类似 default:"..." 的标签为结构体字段设置默认值。标签用于其他用途,比如序列化,但它们不提供设置默认值的机制。

如果你想为 Dog struct 中嵌入的 Animal struct 的字段设置默认值,有几种选择:

1) 构造函数

你可以为 Dog struct 创建一个构造函数,该构造函数使用你想要的默认值初始化嵌入的 Animal struct。

type Animal struct {
    Name string
    Ears int
    Eyes int
}

type Dog struct {
    Animal
}

// NewDog 创建一个具有 Animal 字段默认值的新 Dog 实例。
func NewDog(name string) *Dog {
    return &Dog{
        Animal: Animal{
            Name: name,
            Ears: 2,
            Eyes: 2,
        },
    }
}

2) 在声明时初始化

你可以在声明 Dog struct 的新实例时直接设置默认值。

type Animal struct {
    Name string
    Ears int
    Eyes int
}

type Dog struct {
    Animal
}

func main() {
    myDog := Dog{
        Animal: Animal{
            Ears: 2,
            Eyes: 2,
        },
    }
    // 如果需要,之后可以设置名称
    myDog.Name = "Buddy"
}
英文:

In the code provided, the struct Dog embeds the Animal struct, and there seems to be an attempt to set default values for the Ears and Eyes fields of the embedded Animal struct. However, the syntax used is not valid for setting default values.

In Go, you cannot directly set default values for struct fields using tags like default:"...". Tags are used for other purposes like serialization, but they don't provide a mechanism for setting default values.

If you want to set default values for the fields of the embedded Animal struct in the Dog struct, you have a couple of options:

1) Constructor function:

You can create a constructor function for the Dog struct that initializes the embedded Animal struct with the default values you desire.

    type Animal struct {
            Name string
            Ears int
            Eyes int
        }
        
        type Dog struct {
            Animal
        }
        
        // NewDog creates a new Dog instance with default values for Animal fields.
        func NewDog(name string) *Dog {
            return &Dog{
                Animal: Animal{
                    Name: name,
                    Ears: 2,
                    Eyes: 2,
                },
            }
        }

2) Initialize on declaration:

You can simply set default values when declaring a new instance of the Dog struct.

        type Animal struct {
            Name string
            Ears int
            Eyes int
        }
        
        type Dog struct {
            Animal
        }
        
        func main() {
            myDog := Dog{
                Animal: Animal{
                    Ears: 2,
                    Eyes: 2,
                },
            }
            // You can set the name afterward if needed
            myDog.Name = "Buddy"
        }

huangapple
  • 本文由 发表于 2023年7月22日 06:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76741633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定