GoLang:内部结构的泛型和动态类型

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

GoLang: Generic In Inner Struct and Dynamic Type

问题

我想创建一个子结构体,使用泛型类型作为变量,根据情况可以是两种类型之一。我有以下解决方案:

  1. type Resource interface {
  2. Phone | Computer
  3. }
  4. type MetricData[R Resource] struct {
  5. ResourceName string `json:"resourceName"`
  6. Metrics []Metric[R] `json:"metrics"`
  7. }
  8. type Metric[R Resource] struct {
  9. Resource R `json:"resource"`
  10. Occurrences int `json:"occurrences"`
  11. Change int `json:"change"`
  12. }

然而,我在运行时不知道Resource的类型。当我创建一个新的结构体实例时,我需要提供R的类型:

  1. model.MetricData[model.Phone]{ResourceName: resource, Metrics: tm}

有没有办法在运行时给MetricData指定类型,并通过if/else语句根据某些条件确定类型?

英文:

I want to make a child struct use a generic type as variable can be one of two types depending on the situation. I have the following solution

  1. type Resource interface {
  2. Phone | Computer
  3. }
  4. type MetricData[R Resource] struct {
  5. ResourceName string `json:"resourceName"`
  6. Metrics []Metric[R] `json:"metrics"`
  7. }
  8. type Metric[R Resource] struct {
  9. Resource R `json:"resource"`
  10. Occurrences int `json:"occurrences"`
  11. Change int `json:"change"`
  12. }

However, I won't know the type of Resource at runtime. When I create a new struct instance I need to provide the type of R

  1. model.MetricData[model.Phone]{ResourceName: resource, Metrics: tm}

Is there a way to give MetricData the type at runtime and determine it via if/else statement depending on some condition?

答案1

得分: 2

也许这会有所帮助(playground):

  1. func resourceType[T Resource](r T) string {
  2. switch any(r).(type) {
  3. case Phone:
  4. return "phone"
  5. case Computer:
  6. return "computer"
  7. default:
  8. return ""
  9. }
  10. }
英文:

May be this will help (playground):

  1. func resourceType[T Resource](r T) string {
  2. switch any(r).(type) {
  3. case Phone:
  4. return "phone"
  5. case Computer:
  6. return "computer"
  7. default:
  8. return ""
  9. }
  10. }

答案2

得分: 0

你可以像这样修改你的结构:

  1. type Resource interface {}
  2. type MetricData struct {
  3. ResourceName string `json:"resourceName"`
  4. Metrics []Metric `json:"metrics"`
  5. }
  6. type Metric struct {
  7. Resource Resource `json:"resource"`
  8. Occurrences int `json:"occurrences"`
  9. Change int `json:"change"`
  10. }

现在,要知道Resource的确切类型,你可以这样做:

  1. dummyMetric := {Resource: models.Phone{}, Occurrence: 1, Change: 1}
  2. resourceType := ""
  3. switch dummyMetric.Resource.(type) {
  4. case models.Phone:
  5. resourceType = "phone"
  6. case models.Computer:
  7. resourceType = "computer"
  8. }

希望对你有所帮助!

英文:

You can modify your structures like this:

  1. type Resource interface {}
  2. type MetricData struct {
  3. ResourceName string `json:"resourceName"`
  4. Metrics []Metric `json:"metrics"`
  5. }
  6. type Metric struct {
  7. Resource Resource `json:"resource"`
  8. Occurrences int `json:"occurrences"`
  9. Change int `json:"change"`
  10. }

Now, to know the exact type of Resource, you can do something like this:

  1. dummyMetric := {Resource: models.Phone{}, Occurrence: 1, Change: 1}
  2. resourceType := ""
  3. switch dummyMetric.Resource.(type) {
  4. case models.Phone:
  5. resourceType = "phone"
  6. case models.Computer:
  7. resourceType = "computer"
  8. }

Hope it helps!

huangapple
  • 本文由 发表于 2023年7月25日 07:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76758667.html
匿名

发表评论

匿名网友

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

确定