Functions

2-minute read
Table of Contents

Functions basically accept values and return values. They are the basic unit of abstraction in Go.

Abstraction is the concept of hiding what happens in the background in order to simplify one’s view of a system. An abstraction is a simplified view of a system which omits unimportant details.

For example, we do not have to know how the Println function in the fmt package actually works to add characters to the console, in order to use Println. This means that the Println function is an abstraction (a simplified view) of the entire process that sends letters and other characters to the console.

Arguments

Return values

Functions as modules

We do not need to know the implementation of the function in order to use the interface that the function provides to us. Anything with an interface and implementation is a module.

The interface is a contract between a system and the environment using that system. A keyboard, for example, is an interface - it allows us (the environment) to enter information into our computer (the system being used) as long as we follow the contract. That is, we use the keys on the keyboard as intending by the designer.

We do not need to understand how a keyboard operates mechanically or how the keyboard interacts with the operating system to fill letters and symbols into a text editor program, before we can use the keyboard to do meaningful work.

Support us via BuyMeACoffee