部分嵌入的结构体

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

Partially embedded struct

问题

我可以将类型A嵌入到B中。

type A struct {
  R int64
  S int64
}

type B struct {
  A
}

但是如何只嵌入一个字段呢?

type B struct {
  A.R // 无法工作
}
英文:

I can embed type A into B.

type A struct {
  R int64
  S int64
}

type B struct {
  A
}

But how do I embed just a single field?

type B struct {
  A.R // does not work
}

答案1

得分: 3

假设你有两个结构体数据类型 AB

如果你想让 AB 都有一个名为 F、类型为 T 的字段,你可以这样做:

type (
	A struct {
		F T
	}

	B struct {
		F T
	}
)

如果你只想在源代码中的一个位置更改类型 T,你可以这样抽象它:

type (
	T = someType

	A struct {
		F T
	}

	B struct {
		F T
	}
)

如果你只想在源代码中的一个位置更改 F 的名称,你可以这样抽象它:

type (
	myField struct {
		F T
	}

	A struct {
		myField
	}

	B struct {
		myField
	}
)

如果你有多个可抽象的字段需要抽象,它们必须分别进行抽象,像这样:

type (
	myField1 struct {
		F1 T1
	}
	myField2 struct {
		F2 T2
	}
	
	A struct {
		myField1
		myField2
	}
	
	B struct {
		myField1
	}
)
英文:

Let's suppose your two struct data types A and B.

If you want A and B to both have a field named F of type T, you can do it like so

type (
	A struct {
		F T
	}

	B struct {
		F T
	}
)

If you wish to change type T in only one location in the source, you can abstract it like so

type (
	T = someType

	A struct {
		F T
	}

	B struct {
		F T
	}
)

If you wish to change the name of F in only one location in the source, you can abstract it like so

type (
	myField struct {
		F T
	}

	A struct {
		myField
	}

	B struct {
		myField
	}
)

If you have multiple extricable fields to abstract, they must be abstracted individually like so

type (
	myField1 struct {
		F1 T1
	}
	myField2 struct {
		F2 T2
	}
	
	A struct {
		myField1
		myField2
	}
	
	B struct {
		myField1
	}
)

答案2

得分: 0

你不能嵌入单个字段。你只能嵌入整个类型。

如果你想嵌入单个字段,你需要创建一个只包含该字段的新类型,然后嵌入该类型:

type R struct {
  R int64
}

type B struct {
  R
}
英文:

You can't embed a single field. You can only embed an entire type.

If you want to embed a single field, you'll need to create a new type that contains only that field, and embed that type instead:

type R struct {
  R int64
}

type B struct {
  R
}

答案3

得分: 0

这是我现在的最佳解决方案...

type A struct {
  R int64
  S int64
}

type B struct {
  R A
}

然后在实现过程中...

&B{
 R: &A{
   R,
   // S, - 理想情况下,我们不应该能够传入 `S`
 }
}

我不喜欢这个解决方案,因为我们仍然可以传入 S...


更新: 根据 @HymnsForDisco 的回答,可以这样编码...

// 可以使用类型定义 `type AR int64`,
//   而不是类型别名(下面的方式),但这将
//   要求我们创建并传递 `&AR{}` 对象,
//   给 `&A{}` 或 `&B{}` 的 `R` 字段。
type AR = int64

type A struct {
  R AR
  S int64
}

type B struct {
  R AR
}

并实现为...

&B{
  R,
}
英文:

This is the best solution I have now...

type A struct {
  R int64
  S int64
}

type B struct {
  R A
}

And then during implementation...

&B{
 R: &A{
   R,
   // S, - ideally we would not be able to pass in `S`
 }
}

I don't like this solution, because we can still pass in S...


Update: Based on @HymnsForDisco answer this could be coded as...

// A type definition could be used `type AR int64`,
//   instead of a type alias (below), but that would
//   require us to create and pass the `&AR{}` object,
//   to `&A{}` or `&B{}` for the `R` field.
type AR = int64

type A struct {
  R AR
  S int64
}

type B struct {
  R AR
}

And implemented as...

&B{
  R,
}

huangapple
  • 本文由 发表于 2022年11月14日 10:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/74426329.html
匿名

发表评论

匿名网友

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

确定