Trying to create adaptor pattern in Golang, Not sure where I am doing wrong. My client.go showing an error
c.broker.placeOrder undefined (type exchange.Exchange has no field or method placeOrder)
main.go
package main
import (
"context"
"oms/consumer"
)
func main() {
ctx := context.Background()
consumer.Consumer(ctx)
}
consumer.go
package consumer
import (
"context"
"fmt"
"oms/broker"
)
func Consumer(ctx context.Context) {
broker.Execute()
}
client.go
package broker
import (
"oms/broker/exchange"
)
type Client struct {
broker exchange.Exchange
}
func (c *Client) SetBroker(broker exchange.Exchange) {
c.broker = broker
}
func (c *Client) placeOrder(id string, quantity, price int) {
// I am getting error here
c.broker.placeOrder(id, quantity, price)
}
broker.go
package broker
// create a Client and set its broker to Paytm
import (
"oms/broker/paytm"
)
func Execute() {
client := &Client{}
client.SetBroker(paytm.Paytm{ /* fields */ })
client.placeOrder("order1", 10, 100)
}
exchange.go
package exchange
type Exchange interface {
placeOrder(id string, quantity, price int)
}
paytm.go
package paytm
import "oms/broker/exchange"
type Paytm struct {
// fields
}
func (p Paytm) placeOrder(id string, quantity, price int) {
// implementation for Paytm's placeOrder method
}
You are trying to call an unexported method from your broker
package.
If you want to call the method from outside of the paytm
package you should rename it to PlaceOrder
in your interface, as well as your method.
More information on exported/unexported fields and methods can be found e.g. here: https://golangbyexample.com/exported-unexported-fields-struct-go/