Cyberithub

Functions in Golang with Examples [Complete Tutorial]

Advertisements

Functions - a piece of code block written to perform a specific task - has made programming easy from the creation of the first functional programming language in the 1950s (LISP) by John McCarthy. Many high level languages use the concept of functions to implement a code sequence for compilers to run. Functions also allow programmers to break a task into smaller tasks. In this article, we will explore the concepts of functions in Go and its peculiarities.

 

Prerequisites

In order to follow through this article:-

Advertisements
  • You need to have a basic understanding of programming.
  • You need to download the Go compiler from the official website and follow the directions to install it based on your operating system being either Windows, MacOS or any of the Linux distributions.
  • You need a code editor like visual studio, atom or sublime text. Alternatively, you can download an Integrated Development Environment (IDE). An IDE is an improved code editor with added qualities customized to help you write and execute codes easier. Goland is the most popular IDE used for developing Go programs.
  • You need to have an understanding of Go basic data types and how to run Go programs.

 

Functions in Golang with Examples [Complete Tutorial]

Functions in Golang with Examples [Complete Tutorial]

Also Read: How to use Structures in Golang [Complete Tutorial]

A function is an initialized piece of code that can be called in different parts of a program to perform a specific task. Every Go program must include at least one function. The most popular function used in every independent Go program is the main() function, which is where the compiler begins code execution.

Advertisements

A function can typically be called in different parts of a program but Go reserves the main() and init() functions for code execution directives. Both functions cannot be called in other parts of a program. The init() function is implicitly declared and run before the compiler begins code execution from main(). Let's show a brief code example of how init() works.

NOTE:

Advertisements
Please note that all code samples in this article were run on a 64-bit Ubuntu 20.04 Linux environment.

Functions in Golang with Examples [Complete Tutorial] 2

Output

Functions in Golang with Examples [Complete Tutorial] 3

 The init() and main() functions are typically declared once within programs and cannot be called from other parts of a program. The init() function is usually used to populate a global variable before a program's execution.

Advertisements

 

Function Declaration

Functions in GO is comprised of :-

  • Func keyword (func): a built-in keyword used to declare a block of code as a function.
  • Function name: an identifier with which the function is known throughout the program.
  • Parameter: zero or more values a function accepts and executes operations on.
  • Return type: indicates the data type of zero or more values that a function returns after its execution.
  • Function body: piece of code within curly braces that specifies the task a function performs.
func function_name(parameters) return_type {
   //function_body
}

The func keyword is the most important part of a function since it is used in every type of function. function_name can be any name that starts with a letter of the alphabet (A-Z/a-z) or an underscore (_). A function name cannot start with a number, therefore a name like "6cars'' is invalid. Function names should usually be descriptive of what the function does, for example, a function that adds two numbers to each other can be named add.

Functions can be defined to accept an infinite number of parameters. A parameter is a name-datatype pair that indicates the name to refer to a parameter within the function and the data type of the argument to be passed in during a  function call. A return type indicates the data type of the values to be returned after a function executes. The return types are placed within an optional bracket if the function is to return more than one value.

The function body is where the programming logic to execute a particular task is written. The function body typically performs some operations on the parameters. If a function returns a value or values, the function body must end with a return statement followed by the value(s) to be returned. Let's show a typical example of a function declaration.

Functions in Golang with Examples [Complete Tutorial] 4

The code snippet above is a function diff that accepts 2 parameters of type int and returns a value of type int also. The function computes the difference between two numbers. We can also group parameters of the same type by indicating the data type once. The function above can also be declared like below.

Functions in Golang with Examples [Complete Tutorial] 5

We can call the function above using its name and a bracket in which values or arguments to be passed are specified. We then assign the result to a variable.

Functions in Golang with Examples [Complete Tutorial] 6

Output

Functions in Golang with Examples [Complete Tutorial] 7

A function may have named return types. In this case, new variables will be initialized and given the zero value of the return types specified. Therefore, we can return from the function using only return. Although this is valid syntax, it is rarely used because it reduces code readability. The code snippet below is similar to the diff we declared above.

Functions in Golang with Examples [Complete Tutorial] 8

Go's standard library provides a number of packages with functions used regularly. A popular package is the fmt package which provides functions like Printf, Println, and Print used for formatting output text or strings. Another package is the os package which provides functions for performing operations on a server's operating system.

 

Passing into Functions by Reference

In the code examples above, we create functions and call them by passing either a constant literal or a variable. In this case, a duplicate of the arguments is created and passed into the function, therefore, any change to the variable will not affect the caller. This concept is called pass by value. Let's demonstrate this below:-

Functions in Golang with Examples [Complete Tutorial] 9

Output

Functions in Golang with Examples [Complete Tutorial] 10

To update the caller variables or arguments from within a function, we can define the function to receive a pointer type. This is called pass by reference. The function will receive a pointer to the variable, dereference it and update its value. This way the change will reflect in the caller variables.

Functions in Golang with Examples [Complete Tutorial] 11

Output

Functions in Golang with Examples [Complete Tutorial] 12

 

Variadic Functions

A function that can accept any number of parameters is called a variadic function. We declare a variadic function by placing the variadic operator (...) before the data type of its parameter. A commonly used variadic function is fmt.Println which can take any number of arguments and prints them to os.Stdout separated by single spaces.

Functions in Golang with Examples [Complete Tutorial] 13

From the snippet above, any is an alias for interface{} which means the function can receive values of different types. The variadic operator placed before any signifies that the function can receive any number of values.

The parameters of a variadic function are interpreted as a slice of the specified type within the function. The compiler declares an underlying array and returns a slice of the whole array into the variadic function. You can pass a slice into a variadic function by suffixing it with the variadic operator(...).

Functions in Golang with Examples [Complete Tutorial] 14

Output

Functions in Golang with Examples [Complete Tutorial] 15

 

Deferred Function Calls

 A deferred function call executes just before its encompassing function returns. In a local scope (within a function), placing the defer keyword before a function call causes the function to be deferred.

defer function_name(parameters)

Functions in Golang with Examples [Complete Tutorial] 16

Output

Functions in Golang with Examples [Complete Tutorial] 17

Multiple calls to defer a function will be executed in reverse order of how they are called.

Functions in Golang with Examples [Complete Tutorial] 18

Output

Functions in Golang with Examples [Complete Tutorial] 19

Defer functions are typically used for pair operations like open and close, lock and unlock, connect and disconnect to ensure that a resource is released after use. The right place for a defer statement to release a resource is after the resource has been acquired. This prevents leaving a resource hanging after its use.

Functions in Golang with Examples [Complete Tutorial] 20

The program above reads from a file passed in during runtime and prints its content to the console. The echoFile() function opens a file resource using os.Open(), checks that no error was returned, then uses a defer statement to close the file resource (f.Close()) immediately before the function exits. This is an example of a pair operation where a defer statement is applied to ensure a resource is released after it’s successfully acquired.

 

Function Values

Functions are first class citizens in Go, meaning they are treated like any other variable. Functions have a type, can be assigned to a variable, passed as parameters into other functions, and returned from a function. Let's go through an example showing how functions are values.

Functions in Golang with Examples [Complete Tutorial] 21

Output

Functions in Golang with Examples [Complete Tutorial] 22

A function is a reference type, therefore it is not comparable and cannot be used as keys of a map. The zero type of a function is nil. A nil function does not have a function body.

 

Anonymous Functions

An anonymous function is declared at package level and does not have an identifier. They are declared using the func keyword, but omitting the function name. The value obtained is called an anonymous function and can be stored into variables or passed into functions. As a convention, a higher order function (a function which returns a function) usually has its return value defined as an anonymous function. A case of an anonymous function where the function is given access to an external variable is called closures or closure function. Closures can be used to demonstrate that Go functions are not just statements, but reference types.

Functions in Golang with Examples [Complete Tutorial] 23

Output

Functions in Golang with Examples [Complete Tutorial] 24

The program above demonstrates an anonymous function acting as a closure. The returned anonymous function increments x and returns its cube at every execution of c(). The function c() has a state before every of its execution. This is why functions are not considered as just a piece of code, but a reference type in Go.

 

Recursive Functions

A recursive function is a function which calls itself. Recursion is a powerful technique to solve many programming problems. A popular example is using recursion to generate the nth value in a Fibonacci series.

Functions in Golang with Examples [Complete Tutorial] 25

Output

Functions in Golang with Examples [Complete Tutorial] 26

 

Errors

From the Go standard library, some functions can possibly go wrong due to an invalid input, while some do not. For example, in the strconv package from the standard library, a call to strconv.Itoa() takes an integer and returns its ASCII string value. Since the function only accepts integers and all possible integer types can be represented as strings, the function cannot go wrong and therefore returns no errors.

But a function like strconv.Atoi() takes in an ASCII string and returns an integer and an error. The error is nil when a valid string with only numeric characters is passed in, and is non-nil when a non-numeric character is contained in the string. We can pass the error value to fmt.Print() to print a descriptive message about the cause of the error.

Functions in Golang with Examples [Complete Tutorial] 27

Output

Functions in Golang with Examples [Complete Tutorial] 28

Go typically wants us to handle an error immediately after it is returned. We handle the error by checking if it's a non-nil value, then gracefully stop the program or perform some other action based on the consequences of the error on the continuing program execution.

Functions in Golang with Examples [Complete Tutorial] 29

Output

Functions in Golang with Examples [Complete Tutorial] 30

 

Conclusion

Functions provide a level of abstraction for programmers. Examples are the functions from Go's standard library which you can use without knowing how they work underneath. Though it is advisable to learn about how functions work underneath to grab more understanding of the Go programming language, it is not a requirement because of Go's detailed documentation for each function.

Leave a Comment