79838104

Date: 2025-12-04 15:51:10
Score: 1
Natty:
Report link

Somthing like that?


type CardType string

const (
    CTypePlastic CardType = "plastic"
    CTypeVirtual CardType = "virtual"
)

type CardUsage string

const (
    CUxBanking  CardUsage = "banking"
    CUxDiscount CardUsage = "discount"
    CUxLoyality CardUsage = "loyality"
    // ... any oather card ux
)

type CardPrepareFunc func() error

type CardTemplate struct {
    cardType    CardType
    cardUx      []CardUsage
    cardPrepare []CardPrepareFunc
}

func (ct *CardTemplate) Print() error {
    for _, pr := range ct.cardPrepare {
        if err := pr(); err != nil {
            return err
        }
    }
    if ct.cardType == CTypePlastic {
        // ct.sendToPrinter()
    }

    return nil
}

type CardOption = func(*CardTemplate) error

func WithCardUsage(cu CardUsage, f CardPrepareFunc) CardOption {
    return func(ct *CardTemplate) error {
        ct.cardUx = append(ct.cardUx, cu)
        ct.cardPrepare = append(ct.cardPrepare, f)

        return nil
    }
}

func NewCardTemplate(opts ...CardOption) *CardTemplate {
    ct := new(CardTemplate)
    for _, opt := range opts {
        opt(ct)
    }

    return ct
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Николай Лубышев