Golang中的适配器模式

huangapple go评论85阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2022年12月18日 15:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/74839813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定