dongzhang6021 2018-05-22 00:48
浏览 54
已采纳

如何通过go例程连接多个切片

New to go. I am trying to read a map[int][]string, write the slice of strings into an intermediate channel and then once everything is written, read back all the strings from intermediate channel to another channel and finally read the channel into another goroutine.

I am not able to figure out what is a good non-blocking way to read from the intermediate channel.

package main

import (
    "fmt"
)

func f1(c chan []string, q chan int) {
  // intermediate channel
    ic := make(chan []string, 10)

  hmap := map[int][]string{
    0: []string{"a", "b", "c"},
    1: []string{"d", "e",},
    2: []string{"f", "g", "h"},
  }
  // for every elem in hmap put the values into intermediate channel
    for _, v := range hmap {
       f2(v, ic)
    }

  // everything is in intermediate channel by now
  // read all the []string and concatenate them into a slice in a
  // non-blocking fashion
  var strs []string
  for v := range ic {
    strs = append(strs, v...)
  }
    // strs := <-ic
  fmt.Println(strs)
    select {
  case c <- strs:
    fmt.Println("Received strings.")
  default:
    fmt.Println("did not receive anything.")
  }
    q <- 1
}

func f2(v []string, ic chan []string) {
    select {
    case ic <- v:
        fmt.Println("Sent to intermediate channel:", v)
    default:
        fmt.Println("nothing to send...")
    }
}
func f3(c chan []string) {
    fmt.Println(<-c)
}

func main() {
    c := make(chan []string, 10)
    q := make(chan int)
    go f1(c, q)
    go f3(c)
    fmt.Println(<-q) // to wait for the quit to be set
}

go run main.go to run.

This program goes into deadlock. How can I avoid the deadlock?

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-05-22 01:55
    关注

    As @zerkms said in the comment, you need to close the channel when you're done writing to it, or the for v := range ic { will block.

    package main
    
    import (
        "fmt"
    )
    
    func f1(c chan []string, q chan int) {
        ic := make(chan []string, 10)
    
        hmap := map[int][]string{
            0: []string{"a", "b", "c"},
            1: []string{"d", "e"},
            2: []string{"f", "g", "h"},
        }
        for _, v := range hmap {
            f2(v, ic)
        }
        // done writing strings, close the channel
        close(ic)
        var strs []string
        for v := range ic {
            strs = append(strs, v...)
        }
        fmt.Println(strs)
        select {
        case c <- strs:
            fmt.Println("Received strings.")
        default:
            fmt.Println("did not receive anything.")
        }
        q <- 1
    }
    
    func f2(v []string, ic chan []string) {
        select {
        case ic <- v:
            fmt.Println("Sent to intermediate channel:", v)
        default:
            fmt.Println("nothing to send...")
        }
    }
    func f3(c chan []string) {
        fmt.Println(<-c)
    }
    
    func main() {
        c := make(chan []string, 10)
        q := make(chan int)
        go f1(c, q)
        go f3(c)
        fmt.Println(<-q) // to wait for the quit to be set
    }
    

    https://play.golang.org/p/qZoAElTlkAa

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接