Different methods to pass parameters to function in GO
In this article, we shall be discussing how to pass parameters to function in Golang, with help of practical examples. A function is a collection of specific parameters to execute a specific task and return desired values or without any return as well.
Golang supports two different ways to pass parameters to function as listed below.
- Pass by value
- Pass by Pointer
Go, uses the call/pass by value way to pass the parameters to the function. Below is a detailed explanation of the above ways.
Pass parameters by value or call by value
Golang supports the pass-by-value of parameters into a function, this simply means the parameters are copied into another location of memory. The original value of the parameters doesn't change, in any modification or access of events a copy is created. All data types in Golang i.e int, string, struct, slice, and floats are passed by value.
Example 1: Pass int as parameter
package main
import (
"fmt"
)
func multiply(n int) int {
return n * n
}
func main() {
val := 3
fmt.Println("Original: ", val)
fmt.Println("Pass value to function ", multiply(val))
fmt.Println("After: ", val)
}
Output:
$ go run main.go
Original: 3
Pass value to function 9
After: 3
Example 2: Pass float as parameter
package main
import (
"fmt"
)
func areaOfTriangle(base, height int) float64 {
return 0.5 * float64(base*height)
}
func main() {
base := 4
height := 3
fmt.Printf("Original: base %v and height is %v \n", base, height)
fmt.Println("Pass value to function ", areaOfTriangle(base, height))
}
Output:
Original: base 4 and height is 3
Pass value to function 6
Example 3: Pass struct as parameter
package main
import (
"fmt"
)
type Company struct {
Name string
Description string
}
func UpdateCompanyDetails(c Company) Company {
c.Name = "GoLinux"
c.Description = "Online Study Gurus"
return c
}
func main() {
res := Company{
Name: "JetBrains",
Description: "Proficicient in IDEs",
}
fmt.Printf("Original struct set values: %v \n", res)
fmt.Println("Pass values to function update: ", UpdateCompanyDetails(res))
fmt.Printf("Original struct set values after execution: %v \n", res)
}
Output:
Original struct set values: {JetBrains Proficient in IDEs}
Pass values to function update: {GoLinux Online Study Gurus}
Original struct set values after execution: {JetBrains Proficient in IDEs}
Explanation: The above examples demonstrate the pass-by-value of the variables in different data types in Golang. You will notice that the original value does not change even after modification.
Pass parameters by pointer
in Golang we can pass a pointer of parameter *variable
in the calling function to the corresponding formal parameter of a function. Pass-By-Pointer allows modification of the values of the variable to which the pointer parameters point. Pass-By-pointer is supported in the following data types channel, structs, and map. Golang doesn't support the Pass-By-Reference of parameters into a function.
Under the hood creation of Map, struct, and channels in Golang should not be confused with Pass-by-reference, instead passing the variable to a function is a Pass-By-Pointer, not a Pass-By-Reference. For instance Struct
contains three components in its structure namely:= a pointer of the array, length of the slice, and capacity for the slice, all this three are pass-by-pointer. Channel and Map share a common concept of using the Make() function which returns results of pointer *hmap
. Its variables are created using res := make(map[type]type)
for maps and channel is make(chan [channel type])
.
Kindly Grab knowledge about Golang struct, Map, channel, pointer, and functions for our previous articles. Below is a series of examples:-
Example 1: Pass Map as parameter
package main
import (
"fmt"
)
func main() {
// initiate map
results := make(map[string]int, 0)
// add net worth for each company
results["Google"] = 300000
results["Chrome"] = 4000
results["GoLinux"] = 80000
results["AWS"] = 60000
fmt.Printf("Original values: %v \n", results)
fmt.Printf("Pass-values to function: %v \n", updateCompanyNet(results))
fmt.Printf("Original values after function execution : %v\n", results)
}
func updateCompanyNet(data map[string]int) map[string]int {
data["Google"] = 9000
data["AWS"] = 2000
return data
}
Output:=
Original values: map[AWS:60000 Chrome:4000 GoLinux:80000 Google:300000]
Pass-values to function: map[AWS:2000 Chrome:4000 GoLinux:80000 Google:9000]
Original values after function execution : map[AWS:2000 Chrome:4000 GoLinux:80000 Google:9000]
Example 2: Pass channel as parameter
package main
import (
"fmt"
)
func sendMessage(msg chan string) {
msg <- "Hello From GoLinux Academy"
}
func main() {
// initialise an empty unbuffered channel
res := make(chan string)
// check if the initial value of the channel
// use goroutine
go sendMessage(res)
// check if the after execution value of the channel
fmt.Printf("Response of channel: %v", <-res)
}
Output:
$go run main.go
Response of channel: Hello From GoLinux Academy
Explanation: In the above examples of channel and map, you will realize that the original values are modified after the execution of the functions. this is due the fact that the variable memory location or address was passed to the function as a parameter pointer.
Summary
Golang is a function-based language. The function makes clarity of code, reusability, no duplicate, simplified and single responsibility. It's clear now that Golang only supports the Pass-By-Value and Pass-By-Pointer of parameters to a function. When parameters are passed by value, the function receives a copy of the parameter and its modification does not affect the original value set.
Using Pass-by-Pointer which is used to modify the underlying param values Thus, Go does not support Pass-By-Reference.
Reference