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.

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

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到