golang switch type based on string key in map

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

golang switch type based on string key in map

问题

我需要根据类型映射的字符串键获取一个类型。假设我有以下类型:

  1. type RequestType struct {
  2. Type string `json:"type"`
  3. //Params map[string]string `json:"params"`//在这里包含参数?Params的内容可能在请求之间有所不同
  4. }
  5. type ParamsRequest struct {
  6. RequestType
  7. Param1 string
  8. Param2 string
  9. }
  10. type OtherRequest struct {
  11. RequestType
  12. Param3 string
  13. Param4 string
  14. }

稍后...

  1. var requestTypes = map[string]protocol.RequestType{
  2. "base": ParamsRequest{}, //这里出错了(无法将ParamsRequest用作RequestType类型)
  3. "base2": OtherRequest{},
  4. }
  5. params := requestTypes["base"] //或者base2

在代码中,我需要访问所有请求类型的公共字段:

  1. myFunction(params.Type)//或者params.Params

或者具体类型的特定字段。所以:

  1. params := requestTypes["base"]
  2. fmt.Println(params.Type)//对所有类型都是公共的
  3. fmt.Println(params.Param1)
  4. fmt.Println(params.Param2)

或者:

  1. params := requestTypes["base2"]
  2. fmt.Println(params.Type)
  3. fmt.Println(params.Param3)
  4. fmt.Println(params.Param4)

或者:

  1. params := requestTypes["base"]
  2. fmt.Println(params.Params.Param1)
  3. fmt.Println(params.Params.Param2)

但是在声明映射时,我得到了以下错误:

无法将ParamsRequest用作RequestType类型

有任何想法将受到欢迎。谢谢。

英文:

I need to get a type based on a string key of types map. Suppose I have the following types:

  1. type RequestType struct {
  2. Type string `json:"type"`
  3. //Params map[string]string `json:"params"`//include params here? Contents of Params may be different among requests
  4. }
  5. type ParamsRequest struct {
  6. RequestType
  7. Param1 string
  8. Param2 string
  9. }
  10. type OtherRequest struct {
  11. RequestType
  12. Param3 string
  13. Param4 string
  14. }

Later on..

  1. var requestTypes = map[string]protocol.RequestType{
  2. "base": ParamsRequest{}, //error here (Cannot use ParamsRequest as the type RequestType)
  3. "base2": OtherRequest{},
  4. }
  5. params := requestTypes["base"] //or base2

And in the code I need to access common fields for all request types:

  1. myFunction(params.Type)//or params.Params

Or params, specific for concrete type.
So:

  1. params := requestTypes["base"]
  2. fmt.Println(params.Type)//common for everyone
  3. fmt.Println(params.Param1)
  4. fmt.Println(params.Param2)

Or:

  1. params := requestTypes["base2"]
  2. fmt.Println(params.Type)
  3. fmt.Println(params.Param3)
  4. fmt.Println(params.Param4)

Or:

  1. params := requestTypes["base"]
  2. fmt.Println(params.Params.Param1)
  3. fmt.Println(params.Params.Param2)

But instead when declaring my map I get the following error:

Cannot use ParamsRequest as the type RequestType

Any idea would be welcome. Thank you.

答案1

得分: 0

如果我正确理解您的要求,您想创建一个由两个Request结构体实现的RequestType接口。以下是一个示例代码:

  1. package main
  2. import "fmt"
  3. type RequestType interface {
  4. Type() string // 一个带有Type函数的接口
  5. }
  6. type ParamsRequest struct {
  7. Param1 string
  8. Param2 string
  9. }
  10. func (p ParamsRequest) Type() string { // 不同的Request类型有不同的Type()逻辑
  11. return fmt.Sprintf("%s,%s", p.Param1, p.Param2)
  12. }
  13. type OtherRequest struct {
  14. Param3 string
  15. Param4 string
  16. }
  17. func (o OtherRequest) Type() string {
  18. return fmt.Sprintf("%s,%s", o.Param3, o.Param4)
  19. }
  20. func main() {
  21. var requestTypes = map[string]RequestType{
  22. "base": ParamsRequest{Param1: "hi", Param2: "bye"}, // 这里有错误(无法将ParamsRequest用作RequestType类型)
  23. "base2": OtherRequest{},
  24. }
  25. myFunction(requestTypes["base"].Type())
  26. }
  27. func myFunction(paramTypeString string) {
  28. fmt.Printf("%s\ndont know ...\n", paramTypeString)
  29. }

希望这可以帮助到您!

英文:

If I understand the requirement correctly here, you want to create an interface of RequestType which will be implemented by both the Request structs. below is a sample code for it.

  1. package main
  2. import "fmt"
  3. type RequestType interface {
  4. Type() string // an interface with Type as a function
  5. }
  6. type ParamsRequest struct {
  7. Param1 string
  8. Param2 string
  9. }
  10. func (p ParamsRequest) Type() string { // different Type() logic for different Request types
  11. return fmt.Sprintf("%s,%s", p.Param1, p.Param2)
  12. }
  13. type OtherRequest struct {
  14. Param3 string
  15. Param4 string
  16. }
  17. func (o OtherRequest) Type() string {
  18. return fmt.Sprintf("%s,%s", o.Param3, o.Param4)
  19. }
  20. func main() {
  21. var requestTypes = map[string]RequestType{
  22. "base": ParamsRequest{Param1: "hi", Param2: "bye"}, //error here (Cannot use ParamsRequest as the type RequestType)
  23. "base2": OtherRequest{},
  24. }
  25. myFunction(requestTypes["base"].Type())
  26. }
  27. func myFunction(paramTypeString string) {
  28. fmt.Printf("%s\ndont know ...\n", paramTypeString)
  29. }

huangapple
  • 本文由 发表于 2022年11月15日 03:42:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74437010.html
匿名

发表评论

匿名网友

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

确定