go - golang channel 中的函数调用

标签 go

我一直在尝试让一个函数在 golang channel “内部”被调用(想想 pythons pool.apply_async,我可以在其中对大量函数进行排队并稍后同时运行它们)。但无济于事。我读过的所有内容都让我相信这应该是可能的,但现在我认为它不是,因为我看到我尝试的任何错误后都出现编译错误。代码如下(应该是自包含和可运行的)

package main

import (
    "fmt"
    "math"
)

type NodeSettings struct {
    Timeout  int
    PanelInt float64
    PanelCCT float64
    SpotInt  float64
    SpotCCT  float64
    FadeTime int
    Port     int
}

func main() {
    fmt.Println("Attempting comms with nodes")

    futures := make(chan func(ip string, intLevel, cctLevel int, ns *NodeSettings), 100)
    results := make(chan int, 100)

    ns := NodeSettings{
        Timeout:  5,
        PanelInt: 58.0,
        PanelCCT: 6800.0,
        SpotInt:  60.0,
        SpotCCT:  2000.0,
        FadeTime: 0,
        Port:     40056,
    }

    spots := []string{"192.168.52.62", ...snipped}

    panels := []string{"192.168.52.39", ...snipped}

    for _, ip := range panels {
        intLevel := math.Round(254.0 / 100.0 * ns.PanelInt)
        cctLevel := math.Round((7300.0 - ns.PanelCCT) / (7300.0 - 2800.0) * 254.0)
        fmt.Printf("IP %s was set to %d (=%d%%) and %d (=%d K)\n",
            ip, int(intLevel), int(ns.PanelInt), int(cctLevel), int(ns.PanelCCT))
        futures <- set6Sim(ip, int(intLevel), int(cctLevel), &ns)
    }

    for _, ip := range spots {
        intLevel := math.Round(254.0 / 100.0 * ns.SpotInt)
        cctLevel := math.Round((6500.0 - ns.SpotCCT) / (6500.0 - 1800.0) * 254.0)
        fmt.Printf("IP %s was set to %d (=%d%%) and %d (=%d K)\n",
            ip, int(intLevel), int(ns.SpotInt), int(cctLevel), int(ns.SpotCCT))
        futures <- set8Sim(ip, int(intLevel), int(cctLevel), &ns)
    }
    close(futures)

    fmt.Println("Complete")
}

func set6Sim(ip string, intLevel, cctLevel int, ns *NodeSettings) int {
    fmt.Println(fmt.Sprintf("Simulated (6) run for IP %s", ip))
    return 1
}

func set8Sim(ip string, intLevel, cctLevel int, ns *NodeSettings) int {
    fmt.Println(fmt.Sprintf("Simulated (8) run for IP %s", ip))
    return 1
}

最初,我的 chan 定义是 make(chan func(), 100) 结果是:

.\nodesWriteTest.go:52:11: cannot use set6Sim(ip, int(intLevel), int(cctLevel), &ns) (type int) as type func() in send
.\nodesWriteTest.go:60:11: cannot use set8Sim(ip, int(intLevel), int(cctLevel), &ns) (type int) as type func() in send

我认为这是由于签名不匹配,但唉,即使签名匹配我仍然会遇到类似的错误:

.\nodesWriteTest.go:51:11: cannot use set6Sim(ip, int(intLevel), int(cctLevel), &ns) (type int) as type func(string, int, int, *NodeSettings) in send
.\nodesWriteTest.go:59:11: cannot use set8Sim(ip, int(intLevel), int(cctLevel), &ns) (type int) as type func(string, int, int, *NodeSettings) in send

开始认为这是不可能的,那么有没有其他方法可以达到同样的目的呢?或者我只是不太正确。谢谢。

最佳答案

那么,您要做的是发送 int 而不是匿名函数 func(),因为您的 set6Simset8Sim 语句都返回 int。这就是编译器向您抛出该错误的原因。

相反,您需要构造一个匿名函数以发送到 channel 中,如下所示:

    futures <- func(ip string, intLevel, cctLevel int, ns *NodeSettings) {
        set6Sim(ip, int(intLevel), int(cctLevel), ns)
    }

您的代码有点难以理解,因为我们不知道您要做什么。因此,即使没有最小的示例,这也有望为您指明正确的方向,无论您要解决什么问题。

关于go - golang channel 中的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871709/

相关文章:

go - 如何将 uint32 转换为字符串?

Golang 3DES 部分解密加密字符串

json - 获取值 JSON 以在 golang 中创建嵌套

golang 导入 github 私有(private)仓库

go - 信号 goroutines 在 channel 关闭时停止

Golang 带有接收器的函数映射

go - XML 文件的部分索引 (Bleve)

go - 在 Golang 中导入 _ "lib/math"是什么?

python - Golang 中 UUID4 的整数表示

go - 欧拉计划 16 - 帮助解决它