How Do You Pass Parameters to a Function in Golang and Assign the Returned Value to a Variable?

Problem scenario
You want to pass parameters to a function and assign the returned value to a parameter using the Go programming language. What should you do?

This assumes that you have installed Golang; if you need assistance with this, see this posting.

1. Create a file called c.go with this as the content:

package main

import "fmt"

// Function that accepts parameters and returns an integer value:
func multiply(x int, y int) int {
        total := 0
        total = x * y
        return total

func main() {
        // Pass integers for an example
        newtotal := multiply(100, 50)

2. Run it like this: go run c.go

Leave a comment

Your email address will not be published. Required fields are marked *