douce1368 2018-10-18 10:09
浏览 15
已采纳

Golang频道中的函数调用

I've been trying to get a function to be called "inside" a golang channel (think pythons pool.apply_async, where I can queue a load of functions and run them concurrently later on). But to no avail. Everything I've read leads me to believe that this should be possible, but now I'm thinking it isn't, as I'm seeing compile error after error for anything I try. The code is below (should be self contained and runnable)

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)
",
            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)
",
            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
}

Originally, my chan definition was make(chan func(), 100) which resulted in:

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

Which I assumed was due to the signature not matching, but alas, even with matching signatures I still get a similar error:

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

Starting to think that this isn't possible, so is there any other way to achieve the same thing? Or have I just not quite got it right. Thanks.

  • 写回答

1条回答 默认 最新

  • doulao2128 2018-10-18 10:20
    关注

    Well, what you're trying to do is send an int instead of an anonymous function func() since your set6Sim and set8Sim statements both return ints. This is why the compiler is throwing you that error.

    Instead, you'll need to structure an anonymous function to be sent into the channel, like so:

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

    Your code is a bit hard to follow since we have no idea what you're trying to do. So without a minimal example, this will hopefully point you in the right direction with whatever you're trying to solve.

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

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败