登录
首页 >  Golang >  Go问答

调用函数在golang通道中

来源:stackoverflow

时间:2024-03-13 11:18:25 264浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《调用函数在golang通道中》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我一直在尝试让一个函数在golang通道“内部”调用(想想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 语句都返回 ints。这就是编译器向您抛出该错误的原因。

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

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

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

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>