gpt4 book ai didi

go - go中通过方法发送到 channel 的值,无法在外部接收

转载 作者:数据小太阳 更新时间:2023-10-29 03:26:15 26 4
gpt4 key购买 nike

我正在尝试实现某种响应式(Reactive) golang 实现。我有一系列观察员。它们只是一堆 channel 。一切都封装在一个包中,其他代码可以在其中订阅和取消订阅。每当创建订单时,都会推送更改。但是我未能在方法中注册 channel 接收。

package rxOrder

import (
"fmt"
"time"

"errors"

"gopkg.in/mgo.v2/bson"
)

// Order This is the sample data structure
type Order struct {
id bson.ObjectId
moldID bson.ObjectId
bomID bson.ObjectId
deviceID bson.ObjectId
userIds []bson.ObjectId
name string
orderType string // withOrder, noOrder, makeUp, test
startTime time.Time
deadline time.Time
volume int32
}

// OrderMutation This is the struct for sending
// mutations to observers
type OrderMutation struct {
order Order
action string
}

// RxOrder This is the node for reactive Order
// management
type RxOrder struct {
orders []Order
observers map[string]chan OrderMutation
}

// init This method initialize RxOrder, including
// orders slice and subscriber map, user cannot
// initialize a RxOrder object more than once
func (rx *RxOrder) init() error {
if len(rx.orders) == 0 && len(rx.observers) == 0 {
rx.orders = make([]Order, 1)
rx.observers = make(map[string]chan OrderMutation)
return nil
}

return errors.New("Cannot reinitialize orders")
}

// subscribe, add observer to list
func (rx *RxOrder) subscribe(key string, ch chan OrderMutation) error {
if _, ok := rx.observers[key]; ok {
return errors.New("Observer already existed")
}

rx.observers[key] = ch

return nil
}

// unsubscribe, delete observer from list
func (rx *RxOrder) unsubscribe(key string) error {
if _, ok := rx.observers[key]; !ok {
return errors.New("Observer does not exist")
}

delete(rx.observers, key)

return nil
}

// createOrder The method for creating an order
func (rx *RxOrder) createOrder(order Order) error {

if !order.id.Valid() {
return errors.New("Invalid order id")
}

if !order.bomID.Valid() {
return errors.New("Invalid bom id")
}

if !order.deviceID.Valid() {
return errors.New("Invalid device id")
}

if !order.moldID.Valid() {
return errors.New("Invalid mold id")
}

if len(order.userIds) < 1 {
return errors.New("Empty users list")
}

for index, userID := range order.userIds {
if !userID.Valid() {
return errors.New(fmt.Sprint("Invalid user id at index: ", index))
}
}

if len(order.name) < 1 {
return errors.New("Empty order name")
}

if order.orderType != "withOrder" && order.orderType != "noOrder" && order.orderType != "makeUp" && order.orderType != "test" {
return errors.New("Wrong order type")
}

if order.startTime.After(order.deadline) {
return errors.New("Deadline cannot come before start time")
}

if order.volume < 1 {
return errors.New("Empty order is not accepted")
}

rx.orders = append(rx.orders, order)

for _, ch := range rx.observers {
ch <- OrderMutation{order, "create"}
}

return nil
}
func TestCreateOrder(t *testing.T) {
orderManagement := RxOrder{}

orderManagement.init()

orderManagement.subscribe("123", make(chan OrderMutation))
orderManagement.subscribe("345", make(chan OrderMutation))
orderManagement.subscribe("768", make(chan OrderMutation))

order := Order{}
order.id = bson.NewObjectId()
order.bomID = bson.NewObjectId()
order.deviceID = bson.NewObjectId()
order.moldID = bson.NewObjectId()
order.name = "iPhone 8+"
order.orderType = "withOrder"
order.volume = 5
order.startTime = time.Now()
order.deadline = order.startTime.AddDate(0, 1, 1)
order.userIds = make([]bson.ObjectId, 1)
order.userIds = append(order.userIds, bson.NewObjectId())

go func(t *testing.T) {
fmt.Println(<-orderManagement.observers["123"])
}(t)

orderManagement.createOrder(order)
//orderManagement.observers["123"] <- OrderMutation{order, "w"}

t.Fail()
}

当我做测试时,上面的代码什么都不打印,但是如果我取消注释行:

orderManagement.observers["123"] <- OrderMutation{order, "w"}

一切正常。看来我无法在方法内对 channel 进行操作。如何使用 in package 封装 channel 操作?

最佳答案

情况取决于以下输出:

createOrder 是否返回错误?

如果 createOrder 有任何错误,那么它不会在 channel 上发送任何消息,因此通过注释该行,您的主要测试函数将退出而不等待。

你没有输出,如果你添加提到的行,你的主要测试函数将等待 channel 操作,直到你的协程收到消息并打印输出。

如果您的 createOrder 没有错误,您将面临竞争条件,因为在除“123”之外的 channel 上发送的消息将永远阻塞您的主要测试功能。

关于go - go中通过方法发送到 channel 的值,无法在外部接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com