Answer the question
In order to leave comments, you need to log in
Is there an easy way to pass a task to Go?
For example, there is a func lul(x int) function that does something. I want to pass a task like lul(5). What are the options purely syntactically to do this?
If this is a difficult question, can you recommend some articles? I am newbie.
Answer the question
In order to leave comments, you need to log in
Closure example:
package main
import (
"fmt"
"math/rand"
"time"
)
type kelvin float64
func measureTemperature(samples int, sensor func() kelvin) { // measureTemperature принимает функцию в качестве второго параметра
for i := 0; i < samples; i++ {
k := sensor()
fmt.Printf("%v° K\n", k)
time.Sleep(time.Second)
}
}
func fakeSensor() kelvin {
return kelvin(rand.Intn(151) + 150)
}
func main() {
measureTemperature(3, fakeSensor) // Передает название функции другой функции
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question