T
T
theNorthWind2021-02-02 02:00:37
go
theNorthWind, 2021-02-02 02:00:37

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

1 answer(s)
W
WinPooh32, 2021-02-02
@WinPooh32

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) // Передает название функции другой функции
}

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question