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.