使用泛型和参数的Go函数

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

Go func with generics and parameters

问题

我是新手,正在学习Golang,之前是使用Node.js开发的。在TypeScript中,可以在函数中使用泛型,并同时传递其他参数,但我想知道在Golang中是否也能实现类似的功能。

例如,在TypeScript中可以这样写:

  1. private async request<T>(query: string, variables: object): Promise<T> {....}

我尝试在Golang中实现类似的功能,但没有成功。我目前的代码如下:

  1. type Mwapp struct {
  2. Debug bool `json:"debug"`
  3. Context context.Context `json:"-"`
  4. Client graphql.Client `json:"-"`
  5. }
  6. type Handler[T any] struct {
  7. caller func(ctx context.Context, client graphql.Client) (*T, error)
  8. operationName string
  9. }
  10. // 我想在这个函数中使用泛型,以便可以多次重用它
  11. func (mwapp *Mwapp) request[PASS_HERE_GENERIC](handler Handler[T]) (*T, error) {
  12. res, err := handler.caller(mwapp.Context, mwapp.Client)
  13. return res, err
  14. }
  15. func (mwapp *Mwapp) GetMyAccount() (*myAccountResponse, error) {
  16. return mwapp.request(Handler[myAccountResponse]{myAccount, "GetMyAccount"})
  17. }
  18. func (mwapp *Mwapp) GetMyApp() (*myMwappResponse, error) {
  19. return mwapp.request(Handler[myMwappResponse]{myApp, "GetMyApp"})
  20. }

希望能得到帮助,实现这个功能。

英文:

I am new to Golang and come from nodejs development. in typescript it is possible to use generic in function while passing other parameters as well but I wonder if i can accomplish similar thing with Golang.

For example in typescript one can use

  1. private async request&lt;T&gt;(query: string, variables: object): Promise&lt;T&gt; {....}

I am trying similar thing in golang with no success. What I have right now is

  1. type Mwapp struct {
  2. Debug bool `json:&quot;debug&quot;`
  3. Context context.Context `json:&quot;-&quot;`
  4. Client graphql.Client `json:&quot;-&quot;`
  5. }
  6. type Handler[T any] struct {
  7. caller func(ctx context.Context, client graphql.Client) (*T, error)
  8. operationName string
  9. }
  10. //i WANT TO USE GENERICS IN THIS FUNCTION SO THAT I CAN REUSE IT MULTIPLE TIMES
  11. func (mwapp *Mwapp) request[PASS_HERE_GENERIC](handler Handler[T]) (*T, error) {
  12. res, err := handler.caller(mwapp.Context, mwapp.Client)
  13. return res, err
  14. }
  15. func (mwapp *Mwapp) GetMyAccount() (*myAccountResponse, error) {
  16. return mwapp.request(Handler[myAccountResponse]{myAccount, &quot;GetMyAccount&quot;})
  17. }
  18. func (mwapp *Mwapp) GetMyApp() (*myMwappResponse, error) {
  19. return mwapp.request(Handler[myMwappResponse]{myApp, &quot;GetMyApp&quot;})
  20. }

Any help to accomplish this will be appreciated.

答案1

得分: 2

声明的方式,request*Mwapp 的一个方法。要使它成为一个泛型函数的唯一方法是使 Mwapp 成为泛型。另一种方法是将 request 声明为一个函数而不是一个方法。然后,您可以使它成为泛型而不使 Mwapp 成为泛型类型:

  1. func request[T](mwapp *Mwapp, handler Handler[T]) (*T, error) {
  2. }
英文:

The way it is declared, request is a method of *Mwapp. The only way to make it a generic function is my making Mwapp generic. Another way of doing this is by declaring request as a function instead of a method. Then you can make it generic without making Mwapp a generic type:

  1. func request[T](mwapp &amp;Mwapp, handler Handler[T]) (*T, error) {
  2. }

huangapple
  • 本文由 发表于 2022年8月26日 21:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/73501997.html
匿名

发表评论

匿名网友

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

确定