为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 中的频道

Golang 中的频道

Go
慕沐林林 2022-06-21 10:57:20
我正在尝试实现一个函数,该函数使用通道向另一个发送不确定数量的变量(float64 类型)。第二个函数将接收并按照接收顺序创建一个切片。这是为我已经开发的系统添加更多功能的练习。这就是为什么以这种方式说明代码的原因。当缓冲区为1时,程序进入死锁。如果我将它增加到 7(= 切片长度),程序运行错误并且不会停止。我理解缓冲区应该等于0或不大于1,以确保不丢失数据和顺序。https://play.golang.org/p/rnLH51GinsG我希望你能帮助我。package mainimport (    "fmt"    "sync")var waitgroup sync.WaitGroup/* This function simulates another that sends anundetermined amount of float64.*/func sendFloat64() <-chan float64 {    channel := make(chan float64, 7) // Buffer = 1 to ensure receipt of the first data before sending the second    defer close(channel)    originalSlice := []float64{90, 180, 270, 300, 330, 358, 359} // It represents an undetermined number of values.    for _, v := range originalSlice {        channel <- v // one value at a time to keep order        fmt.Println("SendFloat64: send data to channel", v)    }    return channel}/* This function receives the values and then it will beincorporated in a slice respecting the order in which they were sent.*/func RecreateSlice() <-chan []float64 {    waitgroup.Add(1)    defer waitgroup.Done()    channelOut := make(chan []float64)    defer close(channelOut)    var slice []float64    for {        dataRecived, ok := <-sendFloat64()        if !ok {            break        }        fmt.Println("RecreateSlice: Data received from channel", dataRecived)        slice = append(slice, dataRecived)    }    channelOut <- slice    return channelOut}
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

如果我正确理解你的问题,我认为这段代码更好。它也确实保证了订单。


package main


import (

    "fmt"

)


// sendFloat64 simulates another that sends an

// undetermined amount of float64.

func sendFloat64(ch chan<- float64) {

    originalSlice := []float64{90, 180, 270, 300, 330, 358, 359}

    for _, v := range originalSlice {

        // send the data in order as soon as possible to the reciver

        ch <- v

        fmt.Println("SendFloat64: send data to channel", v)

    }

    close(ch)

}


// RecreateSlice receives the values and then it will be

// incorporated in a slice respecting the order in which they were sent.

func RecreateSlice() []float64 {

    var stream []float64

    // Create a float64 channel with a buffer of 1

    channel := make(chan float64, 1)

    go sendFloat64(channel)

    // Append data until the channel is closed

    for data := range channel {

        fmt.Println("RecreateSlice: Data received from channel", data)

        stream = append(stream, data)

    }

    return stream

}


func main() {

    result := RecreateSlice()

    fmt.Println("MainSlice: ", result)

}

输出:


RecreateSlice: Data received from channel 90

SendFloat64: send data to channel 90

SendFloat64: send data to channel 180

SendFloat64: send data to channel 270

RecreateSlice: Data received from channel 180

RecreateSlice: Data received from channel 270

RecreateSlice: Data received from channel 300

SendFloat64: send data to channel 300

SendFloat64: send data to channel 330

SendFloat64: send data to channel 358

RecreateSlice: Data received from channel 330

RecreateSlice: Data received from channel 358

RecreateSlice: Data received from channel 359

SendFloat64: send data to channel 359

MainSlice:  [90 180 270 300 330 358 359]

注意 SendFloat64 的顺序:

90, 180, 270, 300, 330, 358, 359

注意 RecreateSlice 的顺序:

90, 180, 270, 300, 330, 358, 359

并且 RecreateSlice 返回的切片是有序的(验证)


您可以根据此代码的想法扩展您的解决方案。我试图对部分代码进行注释,以便您理解。


查看完整回答
反对 回复 2022-06-21
  • 1 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号