A function
is a group of statements that can be used repeatedly in a program. Below is a simple function definition in Go
language :
func helloUser() {
fmt.Println(" Hello, World!")
}
The above-defined function helloUser() has a print statement in it and It runs it when we call the function.
A function starts with the keyword func followed by the function name. Below is a function that is defined with parameters.
func addNumber(num1 int, num2 int) int {
return num1 + num2
}
The above function has two parameters num1 and num2 and there's a return type specified after the parentheses. The return type here is Int. A Go function can return multiple values
Since the above function has the parameters with the same datatype we can write it as :
func addNumber(num1, num2 int)
Note : Go uses Pass by value
to pass arguments in a function. It cannot change the arguments within the function that are being passed.
Below is an example of multiple return values in a Go function.
package main
import "fmt"
func main() {
num1 := 20
num2 := 30
add, mul := addandMultiplyNum(num1,num2)
fmt.Println("Result after adding is :", add)
fmt.Println("Result after multiplying is :" ,mul)
}
func addandMultiplyNum(num1,num2 int) (int,int){
result1 := num1 + num2
result2 := num1 * num2
return result1, result2
}
Pointers
As I said above Go uses Pass by value in functions. Go is a Pass by value
language.
It means that it only passes the value of the argument and the argument itself. The changes that take place in function stay within that.
To change the values we use Pointers.
A Pointer is a variable that is used to store the memory address of another variable. Every time we use a variable we retrieve the value stored at the variable's address. To access the address of the variable we use &
operator.
Below is the example for that:
var num = 55
fmt.Println(&num) // Prints the address of num
To store the addresses we use pointers. The*
operator is used to declare a pointer.
An example of that is :
var num = 55
var pointertoNum *int = &num
fmt.Println(pointertoNum) //Prints the address of num
If we'd want to store a different value in the variable, We can access the value stored in the address by the*
operator. That is also called dereferencing.
var num = 55
var pointertoNum *int = &num
*pointertoNum = 45
fmt.Println(num) // Prints 45
To change values that are passed to function, We can use Pointers. Below is a example for that :
package main
import "fmt"
func changeNumtoZero(numptr *int){ //The parameter here is pointer to num
*numptr = 0
}
func main() {
num := 20
fmt.Println(num) // Prints 20
changeNumtoZero(&num) //Pass the address of num
fmt.Println(num) // Prints 0
}
That's It for this blog โ .
For more information, You can always check out the official documentation. This is the official site ๐ฏ.
If you are new to the Go language and you want to check out more, Below are some resources ๐ฅ.
Resources
Free coding exercises(Gophercises) course in Go by John Calhoun. You can check it out here.
A full video course on freecodecamp.
There's a course on Codecademy on Go language. You can check it out here.