Golang / GoLang Interfaces and Object Oriented Interview Questions
Walk through a complete Go OOP design: payment processing without inheritance.
A realistic scenario showing how Go's interfaces, composition, and implicit satisfaction replace classical OOP: a payment processing system that is extensible, testable, and loosely coupled — without a single inheritance relationship.
// ── Interfaces ──────────────────────────────────────────────────
type PaymentProcessor interface {
Process(amount float64, currency string) (txID string, err error)
}
type Refunder interface {
Refund(txID string, amount float64) error
}
type FullPayment interface {
PaymentProcessor
Refunder
}
// ── Concrete implementations ────────────────────────────────────
type StripeProcessor struct{ apiKey string }
func NewStripe(key string) *StripeProcessor { return &StripeProcessor{apiKey: key} }
func (s *StripeProcessor) Process(amount float64, currency string) (string, error) {
return "stripe_tx_" + uuid(), nil // call Stripe API
}
func (s *StripeProcessor) Refund(txID string, amount float64) error {
return nil // call Stripe refund API
}
// Compile-time check
var _ FullPayment = (*StripeProcessor)(nil)
// ── Service: depends on narrow interfaces ───────────────────────
type OrderService struct {
payments PaymentProcessor // only needs to charge
}
func (os *OrderService) PlaceOrder(amount float64) error {
txID, err := os.payments.Process(amount, "USD")
if err != nil { return fmt.Errorf("payment failed: %w", err) }
log.Printf("order paid, tx=%s", txID)
return nil
}
type RefundService struct {
refunds Refunder // only needs to refund
}
func (rs *RefundService) Refund(txID string, amount float64) error {
return rs.refunds.Refund(txID, amount)
}
// ── Wiring (main) ────────────────────────────────────────────────
stripe := NewStripe(os.Getenv("STRIPE_KEY"))
orderSvc := &OrderService{payments: stripe} // stripe as PaymentProcessor
refundSvc := &RefundService{refunds: stripe} // stripe as Refunder
// ── Test mock ────────────────────────────────────────────────────
type MockPayment struct{ txID string; err error }
func (m *MockPayment) Process(float64, string) (string, error) { return m.txID, m.err }
func TestPlaceOrder(t *testing.T) {
svc := &OrderService{payments: &MockPayment{txID: "mock_tx"}}
if err := svc.PlaceOrder(99.99); err != nil {
t.Fatal(err)
}
}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
