tour icon indicating copy to clipboard operation
tour copied to clipboard

tour: function values

Open PythonCoderUnicorn opened this issue 1 year ago • 0 comments

Context: https://go.dev/tour/moretypes/24

the example shown in the code editor is not an ideal example for beginners, and I do find it confusing.

func compute(fn func(float64, float64) float64) float64 {
	return fn(3, 4)
}

func main() {

	hypot := func(x, y float64) float64 {
		return math.Sqrt(x*x + y*y)
	}
	fmt.Println(hypot(5, 12))

	fmt.Println(compute(hypot))
	fmt.Println(compute(math.Pow))
}

Could the example be simpler?

I have a simpler one that gets the lesson of the function using values and can be used as a variable

func modFunc(a,b int) int {
	return a % b
}

func main() {
	mod := modFunc
	result := mod(8,3)
	fmt.Println( result )

}

PythonCoderUnicorn avatar Feb 19 '24 22:02 PythonCoderUnicorn