golang switch type based on string key in map

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

golang switch type based on string key in map

问题

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

type RequestType struct {
	Type string `json:"type"`
	//Params map[string]string `json:"params"`//在这里包含参数?Params的内容可能在请求之间有所不同
}

type ParamsRequest struct {
	RequestType
	Param1 string
	Param2 string
}

type OtherRequest struct {
	RequestType
	Param3 string
	Param4 string
}

稍后...

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

params := requestTypes["base"] //或者base2

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

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

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

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

或者:

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

或者:

params := requestTypes["base"]
fmt.Println(params.Params.Param1)
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:

type RequestType struct {
  Type string `json:"type"`
  //Params map[string]string `json:"params"`//include params here? Contents of Params may be different among requests
}

type ParamsRequest struct {
 RequestType
 Param1 string
 Param2 string
}

type OtherRequest struct {
  RequestType
  Param3 string
  Param4 string
}

Later on..

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

params := requestTypes["base"] //or base2

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

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

Or params, specific for concrete type.
So:

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

Or:

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

Or:

params := requestTypes["base"]
fmt.Println(params.Params.Param1)
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接口。以下是一个示例代码:

package main

import "fmt"

type RequestType interface {
	Type() string // 一个带有Type函数的接口
}

type ParamsRequest struct {
	Param1 string
	Param2 string
}

func (p ParamsRequest) Type() string { // 不同的Request类型有不同的Type()逻辑
	return fmt.Sprintf("%s,%s", p.Param1, p.Param2)
}

type OtherRequest struct {
	Param3 string
	Param4 string
}

func (o OtherRequest) Type() string {
	return fmt.Sprintf("%s,%s", o.Param3, o.Param4)
}

func main() {
	var requestTypes = map[string]RequestType{
		"base":  ParamsRequest{Param1: "hi", Param2: "bye"}, // 这里有错误(无法将ParamsRequest用作RequestType类型)
		"base2": OtherRequest{},
	}

	myFunction(requestTypes["base"].Type())
}

func myFunction(paramTypeString string) {
	fmt.Printf("%s\ndont know ...\n", paramTypeString)
}

希望这可以帮助到您!

英文:

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.

package main

import "fmt"

type RequestType interface {
	Type() string // an interface with Type as a function
}
  
type ParamsRequest struct {
	Param1 string
	Param2 string
}
func (p ParamsRequest) Type() string { // different Type() logic for different Request types
	return fmt.Sprintf("%s,%s", p.Param1, p.Param2)
}
  
type OtherRequest struct {
	Param3 string
	Param4 string
}
func (o OtherRequest) Type() string {
	return fmt.Sprintf("%s,%s", o.Param3, o.Param4)
}

func main() {
	var requestTypes = map[string]RequestType{
		"base": ParamsRequest{Param1: "hi", Param2: "bye"}, //error here (Cannot use ParamsRequest as the type RequestType)
		"base2": OtherRequest{},
	}

	myFunction(requestTypes["base"].Type())
}

func myFunction(paramTypeString string) {
	fmt.Printf("%s\ndont know ...\n", paramTypeString)
}

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:

确定