我在想是否有一种方法可以在Go中实现多个带有相同函数名的构造函数,就像Java中那样。另一个选项可能是只有一个带有可选参数的构造函数,但我不确定如何确切地做到这一点。
type Query struct {
TagsQuery string
PageQuery string
}
// First Constructor
func NewQuery(TagsQuery string) Query {
return Query{
TagsQuery: TagsQuery,
PageQuery: "0", // default to first page
}
}
// Second Constructor
func NewQuery(TagsQuery string, PageQuery string) Query {
return Query{
TagsQuery: TagsQuery,
PageQuery: PageQuery,
}
}
第一个构造函数接受一个参数TagsQuery
,并将PageQuery
默认值设为0
。第二个构造函数接受两个参数:TagsQuery
和PageQuery
。
NewQuery
和NewQueryWithPage
呢? - Luke Joshua Park