DEV Community

Cover image for Function values in Go
bluepaperbirds
bluepaperbirds

Posted on

4 1

Function values in Go

In the Go programming language, functions are values. That means that you can pass a function as parameter in a function.

For example, you can define a function mul which multiplies x and y. Then it returns the result.

mul := func(x, y float64) float64 {
    return x*y
}

Then you could assign the output to a variable and output it

var a float64
a = mul(3,5)
fmt.Println(a)

However, you can directly output it as a result to the print function

fmt.Println(mul(5, 12))

The program would then be:

package main

import (
    "fmt"
)

func main() {
    mul := func(x, y float64) float64 {
        return x*y
    }
    fmt.Println(mul(5, 12))
}

This results in:

60

Program exited.

Related links:

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay