推断结构体结构

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

Infer struct structure

问题

我正在使用这个来创建一个没有定义单独类型的结构体:

data := struct {
	Product *domain.UserProduct
	Options *[]domain.UserProductOption
}{
	Product: userProduct,
	Options: userProductOptions,
}

有没有一种方法可以在不定义struct的结构的情况下做到同样的事情,因为字段的数量和类型可以被推断出来?类似这样:

data := {
	Product: userProduct,
	Options: userProductOptions,
}
英文:

I am using this to create a struct without defining a separate type:

data := struct {
	Product *domain.UserProduct
	Options *[]domain.UserProductOption
}{
	Product: userProduct,
	Options: userProductOptions,
}

Is there a way to do the same without defining struct's structure, as number of fields and their types can inferred? Something like:

data := {
	Product: userProduct,
	Options: userProductOptions,
}

答案1

得分: 2

截至Go 1.17版本,还没有类似的方法来推断结构体的类型。

在提案#35304中对此进行了一些讨论,但目前还没有确定。总结一下讨论的内容:

  • foo({x: y}) - 难以阅读
  • data := _{x: y} - 难以阅读 (?)
  • data := struct {x: y} - 过载了struct的语法
  • data := tuple {x: y} - 新的关键字
  • . . .

欢迎您参与讨论,或者提交您自己的提案。

我认为像data := struct _ {X: x, Y: y}这样的写法应该最符合使用_来省略内容的哲学(在这种情况下,我们希望省略结构体定义)。

英文:

As of Go 1.17 there's nothing like that to infer the type of a struct.

There's been some discussion of this in the proposal #35304, but it's still open. To summarize the discussion:

  • foo({x: y}) - unreadable
  • data := _{x: y} - unreadable (?)
  • data := struct {x: y} - overloads the syntax of struct
  • data := tuple {x: y} - new keyword
  • . . .

You're welcome to participate in the discussion and/or submit your own proposal.

I would think something like data := struct _ {X: x, Y: y} should be the most in line with the philosophy of _ being used to omit things (as in this case we want to omit the struct definition).

答案2

得分: 1

没有。该语言不允许这样做:在复合字面量中,定义了它们的工作原理:

复合字面量用于构造结构体、数组、切片和映射的值,并在每次评估时创建一个新值。**它们由字面量的类型后跟一个用大括号括起来的元素列表组成。**每个元素可以选择由相应的键前导。

CompositeLit  = LiteralType LiteralValue .

LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
                SliceType | MapType | TypeName .

LiteralValue  = "{" [ ElementList [ "," ] ] "}" .

可以使用字面量值的地方是在元素列表中:

// 结构体类型可以是定义的或字面量

// 定义的类型
type User struct {
   Name string
}

// `{ "John" }` 是字面量值
foo := []User{{ "John" }}

// 字面量类型
// `{ "John", "john@myself.com" }` 是字面量值
bar := []struct{Name string; Email string}{{ "John", "john@myself.com" }}
英文:

> Is there a way to do the same without defining struct's structure

No. The language doesn't allow it: in Composite literals, it is defined how these work:

> Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated. They consist of the type of the literal followed by a brace-bound list of elements. Each element may optionally be preceded by a corresponding key.

CompositeLit  = LiteralType LiteralValue .

LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
                SliceType | MapType | TypeName .

LiteralValue  = "{" [ ElementList [ "," ] ] "}" .

The place where you can have the literal value is in an element list:

// the struct type may be defined or literal

// defined type
type User struct {
   Name string
}

// `{"John"}` is the literal value
foo := []User{{"John"}}

// literal type
// `{"John","john@myself.com"}` is the literal value
bar := []struct{Name string; Email string}{{"John","john@myself.com"}}

huangapple
  • 本文由 发表于 2021年10月1日 17:29:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/69403604.html
匿名

发表评论

匿名网友

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

确定