golang protobuf 无法在通用类型中进行解组。

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

golang protobuf can not unmarshal in a generic type

问题

type Msg[T any] interface {
	*T
	proto.Message
}

func Handle[T any, U Msg[T]](cb func(req U) (proto.Message, error)) {
	msg := new(T)
	if err := proto.Unmarshal([]byte{}, msg); err != nil {
	}
	_, _ = cb(msg)
}

func main() {
	Handle(doSomething)
}

func doSomething(req *pb.Hello) (proto.Message, error) {
	_ = proto.Unmarshal([]byte{}, req)
	return nil, nil
}

为什么在 Handle 泛型函数中,proto.Unmarshal 不能将 'msg'(类型为 *T)用作 Message 类型?

如何在泛型函数中使用 new(T) 与 protobuf?

英文:
type Msg[T any] interface {
	*T
	proto.Message
}

func Handle[T any, U Msg[T]](cb func(req U) (proto.Message, error)) {
	msg := new(T)
	if err := proto.Unmarshal([]byte{}, msg); err != nil {
	}
	_, _ = cb(msg)
}

func main() {
	Handle(donSomething)
}

func doSomething(req *pb.Hello) (proto.Message, error) {
	_ = proto.Unmarshal([]byte{}, req)
	return nil, nil
}

why proto.Unmarshal Cannot use 'msg' (type *T) as the type Message in Handle generic funcion.

how can i use new(T) in a generic funcion with protobuf

答案1

得分: 2

在你的程序中,T受到any的限制,所以指针类型*T与protobuf类型没有任何关系。它只是一个指向未指定类型的指针。

相反,使用以下方式:

msg := U(new(T))

这样,你就有了一个非nil的指针,指向被推断为pb.HelloT类型。new(T)创建了一个指向pb.Hello零值的指针,而转换U()告诉编译器*T确实满足proto.Message接口。

英文:

In your program T is constrained by any, so the pointer type *T bears no relation to the protobuffer type anymore. It's just a pointer to an unspecified type.

Instead use:

msg := U(new(T))

This way you have a non-nil pointer to whatever T is inferred to — here it will be pb.Hello, new(T) creates a pointer to a pb.Hello zero value, and the conversion U() tells the compiler that *T really satisfies the proto.Message interface.

huangapple
  • 本文由 发表于 2023年8月9日 13:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864765.html
匿名

发表评论

匿名网友

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

确定