dimanche 29 janvier 2023

What is the best pattern to create structs and grant resources

I am curious about is the a good pattern to create golang structs with the ability to grant resources (db, client, etc)

In my case I have a struct called GetUser with the following fields

type GetUser struct {
  DB  *sql.Conn
  Req model.GetUserReq
}

where DB is the connection to postgres. Also I have a struct which have a grpc client to make a HTTP request.

type GetUserTweets struct {
  DB  *sql.Conn
  Client grpc.Client
  Req model.GetUserTweetsReq
}

The thing is the structure GetUserTweets will be created only once GetUser finished successfully, and I have a question is the a good pattern for that, because when GetUserTweets finishes I want to create another struct let's say with SQL connection, redis Connection but without grpc Client. Should I provide all my structures with all possible fields and create something like that and pass it to another my struts ?

type GetUser struct {
  DB  *sql.Conn
  Redis *redis.Conn
  TweeterClient grpc.Client
  InstagramClient grpc.Client
  Req model.GetUserReq
}

Aucun commentaire:

Enregistrer un commentaire