以下问题有多种答案/技巧:
如何将默认值设置为golang结构? 如何初始化结构在golang
我有几个答案,但还需要进一步讨论。
以下问题有多种答案/技巧:
如何将默认值设置为golang结构? 如何初始化结构在golang
我有几个答案,但还需要进一步讨论。
当前回答
做这样的东西怎么样:
// Card is the structure we work with
type Card struct {
Html js.Value
DefaultText string `default:"html"` // this only works with strings
}
// Init is the main function that initiate the structure, and return it
func (c Card) Init() Card {
c.Html = Document.Call("createElement", "div")
return c
}
然后将其命名为:
c := new(Card).Init()
其他回答
其中一种方法是:
// declare a type
type A struct {
Filed1 string
Field2 map[string]interface{}
}
因此,每当你需要一个自定义类型的新变量时,只需调用NewA函数,你也可以将函数参数化,可选地将值分配给struct字段
func NewA() *A {
return &A{
Filed1: "",
Field2: make(map[string]interface{}),
}
}
从https://golang.org/doc/effective_go.html # composite_literals:
有时0值还不够好,需要初始化构造函数,就像这个从包os派生的例子一样。
func NewFile(fd int, name string) *File {
if fd < 0 {
return nil
}
f := new(File)
f.fd = fd
f.name = name
f.dirinfo = nil
f.nepipe = 0
return f
}
结构体
使这个程序更好的一个简单方法是使用结构体。struct是一种包含命名字段的类型。例如,我们可以这样表示一个圆:
type Circle struct {
x float64
y float64
r float64
}
type关键字引入了一个新类型。后面跟着类型的名称(Circle)、关键字struct(表示我们正在定义结构类型)和花括号内的字段列表。每个字段都有一个名称和类型。像函数一样,我们可以折叠具有相同类型的字段:
type Circle struct {
x, y, r float64
}
初始化
我们可以用多种方式创建Circle类型的实例:
var c圆 与其他数据类型一样,这将创建一个默认设置为0的本地Circle变量。对于结构体,0表示每个字段都被设置为相应的0值(0表示整型,0.0表示浮点数,""表示字符串,nil表示指针,…)
c := new(Circle)
这将为所有字段分配内存,将每个字段设置为0值并返回一个指针。(*圆圈)更多时候,我们想给每个字段一个值。我们可以用两种方法做到这一点。是这样的:
c := Circle{x: 0, y: 0, r: 5}
或者,如果我们知道字段的定义顺序,我们可以省略字段名:
c := Circle{0, 0, 5}
type Config struct {
AWSRegion string `default:"us-west-2"`
}
我发现这个帖子很有帮助,也很有教育意义。其他答案已经提供了很好的指导,但我想用一个易于参考的方法总结我的结论(即复制粘贴):
package main
import (
"fmt"
)
// Define an interface that is exported by your package.
type Foo interface {
GetValue() string // A function that'll return the value initialized with a default.
SetValue(v string) // A function that can update the default value.
}
// Define a struct type that is not exported by your package.
type foo struct {
value string
}
// A factory method to initialize an instance of `foo`,
// the unexported struct, with a default value.
func NewFoo() Foo {
return &foo{
value: "I am the DEFAULT value.",
}
}
// Implementation of the interface's `GetValue`
// for struct `foo`.
func (f *foo) GetValue() string {
return f.value
}
// Implementation of the interface's `SetValue`
// for struct `foo`.
func (f *foo) SetValue(v string) {
f.value = v
}
func main() {
f := NewFoo()
fmt.Printf("value: `%s`\n", f.GetValue())
f.SetValue("I am the UPDATED value.")
fmt.Printf("value: `%s`\n", f.GetValue())
}