英文:
Adaptor pattern in golang
问题
尝试在Golang中创建适配器模式,不确定我在哪里出错了。我的client.go显示了一个错误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) {
// 这里出错了
c.broker.placeOrder(id, quantity, price)
}
broker.go
package broker
// 创建一个Client并将其broker设置为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) {
// Paytm的placeOrder方法的实现
}
英文:
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
}
答案1
得分: 1
你正在尝试从你的broker
包中调用一个未导出的方法。
如果你想从paytm
包的外部调用该方法,你应该将它在接口中重命名为PlaceOrder
,以及你的方法。
关于导出/未导出的字段和方法的更多信息可以在这里找到:https://golangbyexample.com/exported-unexported-fields-struct-go/
英文:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论