Your question was already answered correctly in the comments, but maybe it helps you to get some more feedback on your code.
Extract sub types from struct(s)
ResponseGigs
is a large struct with multiple sub-structs which makes it hard to work with. Extracting the sub-structs as extra types makes things easier.
type ResponseGigs struct {
SectionName string `json:"section_name"`
Offset int `json:"offset"`
Limit int `json:"limit"`
TotalRows int `json:"total_rows"`
Gigs []Gig `json:"gigs"`
}
type Gig struct {
SellerInfo SellerInfo `json:"seller_info"`
ID int `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Medias []Media `json:"medias"`
Price int `json:"price"`
Currency string `json:"currency"`
Rating Rating `json:"rating"`
Favorite bool `json:"favorite"`
}
type SellerInfo struct {
UserID int `json:"user_id"`
Username string `json:"username"`
Name string `json:"name"`
ImageProfile string `json:"image_profile"`
Level string `json:"level"`
}
type Media struct {
ID int `json:"id"`
Name string `json:"name"`
TypeFile string `json:"type_file"`
ImageURL string `json:"image_url"`
}
type Rating struct {
AVGRating float64 `json:"avg_rating"`
TotalReviews int `json:"total_reviews"`
}
type TempGig struct {
Id int `json:"id" db:"id"`
Title string `json:"title" db:"title"`
UserID int `json:"user_id" db:"user_id"`
Price int `json:"price" db:"price"`
Currency string `json:"currency" db:"currency"`
Username string `json:"username" db:"username"`
ImageProfile string `json:"image_profile" db:"image_profile"`
Level string `json:"level" db:"level"`
GigRating float64 `json:"gig_rating" db:"gig_rating"`
TotalReview int `json:"total_review" db:"total_review"`
CreatedAt int `json:"created_at" db:"created_at"`
Favorite bool `json:"favorite" db:"favorite"`
}
Create an extra function to transform TempGig
to Gig
Next I'd do is create a function to convert a TempGig
to a Gig
. (I renamed TempGigs
to TempGig
as the struct only holds a single gig, not multiple):
func toGig(in TempGig) Gig {
return Gig{
SellerInfo: SellerInfo{
UserID: in.UserID,
Name: in.Username,
ImageProfile: in.ImageProfile,
Level: in.Level,
},
ID: in.Id,
Title: in.Title,
// ...
}
}
Fill the response slice
To keep the handler code minimal, I'd also create an extra function for building the ResponseGigs
struct. For exammple:
func toResponse(section string, in []TempGig) ResponseGigs {
var gigs []Gig
// or to preallocate the memory space / capacity (not the length!)
// gigs := make([]Gig, 0, len(in))
for _, tempGig := range in {
gigs = append(gigs, toGig(tempGig))
}
return ResponseGigs{
SectionName: section,
Gigs: gigs,
}
}
Alternatively you can preallocate the length of the slice and work with indexes. I prefer the append
approach as it is less error prone.
// preallocate the length of the slice (not only the capacity)
gigs := make([]Gig, len(in))
for i, tempGig := range in {
gigs[i] = toGig(tempGig)
}
Handler code
Finally the handler code would boil down to something like this:
tempResp := toReponse("Best seller", tempGigs)
tempResp.Offset = 0
tempResp.Limit = 10
utils.HTTPJsonSuccess(w, http.StatusOK, tempResp)
return
Hope this helps as a next step. There are many things that can be adjusted to your liking. Happy Coding!