I have 3 structs in one application. Those are
type LoginResponse struct {
ResultResponse
Token string json:"Token"
ComercioId string json:"ComercioId"
UsuarioId string json:"UsuarioId"
}
type DepositoPagoResponse struct {
ResultResponse
TransaccionID int json:"TransaccionId"
Producto string json:"Producto"
FechaTransaccion time.Time json:"FechaTransaccion"
MontoTransaccion float64 json:"MontoTransaccion"
}
type VerifyAccountResponse struct {
ResultResponse
Producto string json:"Producto"
Saldo float64 json:"Saldo"
}
type ResultResponse struct {
IsSuccess bool json:"IsSuccess"
Codigo int json:"Codigo"
Mensaje string json:"Mensaje"
}
To set properties in my structs i wrote methods that works like builder pattern. For example
func (r *VerifyAccountResponse) WithSuccess(success bool) *VerifyAccountResponse {
r.IsSuccess = success
return r
}
func (r *VerifyAccountResponse) WithCodigo(codigo int) *VerifyAccountResponse {
codigo, mensaje := GetCodeMessage(codigo)
r.Codigo = codigo
r.Mensaje = mensaje
return r
}
So i wrote soemthing like
r := &LoginResponse{}
r.WithSuccess(true).WithCode(100).WithToken(“123”) and so on. The methods WithSuccess and WithCode are repeated by each struct.
I would like to wrote those methods only one time for the ResultResponse in each parent struct.
I thinking something like a generic function receiver or something like that to avoid wrote those methods for each parent struct.
Is it possible ? If so, please some hints would be useful…
TIA,
Yamil