Functions
What is a function?
a function is a block of code that performs a specific task. For example, the following code defines a function named greet
that prints a greeting to the console:
main.go | |
---|---|
Declaring functions
Declaring a function is done using the func
keyword. The syntax for declaring a function is as follows:
you can also declare functions using the var keyword in the same style as javascript:
main.go | |
---|---|
Calling functions
Calling functions can be done by using the function name followed by parentheses. The syntax for calling a function is as follows:
main.go | |
---|---|
Function parameters
Function parameters are very important, they allow you to pass data into a function. The syntax for declaring a function with parameters is as follows:
main.go | |
---|---|
Function return values
Single return value
Functions can return values. The syntax for declaring a function with a return value is as follows:
main.go | |
---|---|
Multiple return values
Functions can return multiple values. The syntax for declaring a function with multiple return values is as follows:
main.go | |
---|---|
JSON Decoding example
here is an example of a function that decodes a json string into a struct:
- we import the
encoding/json
package to decode the json string. - we define a
Person
struct with aName
andAge
field. and we use thejson
struct tag to tell thejson.Unmarshal
function how to decode the json string. - The
Decode
function takes a string as input and returns aPerson
and anerror
. - The
Decode
function uses thejson.Unmarshal
function to decode the string into aPerson
struct. - The
Decode
function returns thePerson
struct and anerror
if there is one. - The
main
function calls theDecode
function and prints thePerson
struct if there is no error.
We will explain structs and struct tags in more detail later.
Function scoping
Functions have their own scope. This means that variables declared inside a function are only accessible inside that function. For example, the following code will not compile because the variable x
is not accessible outside the function main
:
The compiler will give the following error message:
Package level functions and exported functions
Scoping functions works in the same way as variables, Upper case functions are exported and lower case functions are not. here is an example:
greeter/greeter.go | |
---|---|
Greet
function is exported and the greet
function is not, hence they are not the same function.
Passing functions as arguments
Functions can be passed as arguments to other functions. The syntax for passing a function as an argument is as follows:
main.go | |
---|---|
Example with a custom type:
main.go | |
---|---|
Example with anonymous functions:
main.go | |
---|---|
- The
main
function calls theprintGreeting
function and passes an anonymous function as an argument. - The anonymous function takes a string as input and returns a string.
- The anonymous function returns a greeting to the console.
Anonymous functions
Anonymous functions are functions without a name. They are useful for creating closures. The syntax for declaring an anonymous function is as follows:
explanation:
- The
main
function calls an anonymous function. - The anonymous function prints "Hello, world!" to the console.
- the
()
at the end of the anonymous function call the function.
Closures
A closure is a function that captures the state of its surrounding environment. For example, the following code defines a function named greet
that captures the variable name
from its surrounding environment:
main.go | |
---|---|
Explanation:
- The
greet
function takes a string as input and returns a function that takes no input and returns nothing. - The returned function captures the variable
name
from its surrounding environment. - The
main
function calls thegreet
function and calls the returned function.
Recursion
Recursion is a technique where a function calls itself. For example, the following code defines a function named factorial
that calculates the factorial of a number:
main.go | |
---|---|
Explanation:
- The
factorial
function takes an integer as input and returns an integer. - The
factorial
function calls itself if the input is not zero and if the the value is not zero it returns the value multiplied by the factorial of the value minus one. - The
main
function calls thefactorial
function.