TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Programming Languages / Software Development

Build Your First Go Function

Both prebuilt and user-defined functions are a necessary components for the Go programming language. Here's what you'll need to understand them.
Jun 8th, 2024 6:00am by
Featued image for: Build Your First Go Function
Feature image by Gerd Altmann from Pixabay.

Functions are an integral part of programming. Without them, building applications would be exponentially more challenging (if not impossible).

Essentially, functions are self-contained modules that accomplish a task. There are typically four types of functions in computer programming: functions with no arguments or return values; functions with no arguments and a return value; functions with arguments and no return values; and functions with arguments and return values.

Nearly every modern programming language includes prebuilt functions that can be used over and over again, to simplify the workflow. Functions serve as the basic building blocks for the Go language. With Go, there are first class functions, higher-order functions, function literals, closures and multiple return values.

One of the more important types of functions for Go is the user-defined function (UDF), which is a function created by the programmer to serve a specific purpose within an application. UDFs can be created once and then called multiple times throughout an application.

A function starts with the func keyword and is generally formatted like so:


That’s pretty standard.

Let’s create a basic function and then call it within the program. Our function will be called add() and will add two integers together. Our function looks like this:


We use the return keyword because we want our function to return the value of a and b.

With our basic function created, it’s now time to call it later on in the application. For our purposes, we’re going to call the function within a main() function. Remember, the main() function is different than the main package, and acts as an entry point for executable code within your programs. In other words, it’s necessary.

Our main() function (which will include the call to our UDF) looks like this:


What we’ve done here is call our function within the Println function (from the fmt package), and defined a as 42 and b as 13. Our entire application looks like this:


Save that file as function.go and run it with the command:


The output should be:

The total of a + b equals 55

What if we wanted to accept input from a user for our function? We can do that as well, with the addition of the scan() function. Let me show you how.

The first portion of our application will look the same:


We then have to modify our main() function to define num1 and num2 as type integer and use both the fmt.Print() and fmt.Scan() functions to first print instructions and then accept input.

We define num1 and num2 like this:


Our first Print/Scan combo is for num1, and looks like this:


What the above two lines does is instruct the user to type a number and then defines num1 as the value typed by the user. We do the same thing for num2 with these two lines:


We then print a final line and then call our add() function with the following:


The entire app looks like this:


Save that file as input.go and run it with this command:


You will asked to type a number for a and then type a number for b. Those two numbers will be added together, and the output will be:

The total of a + b equals X

Where X is the result of the two inputted numbers.

We could also condense that by taking multiple inputs with fmt.Scan(). For that, we’d alter the function like so:


We still call the function in the same manner. The only thing that’s changed is how we take input from the user.

Here’s another fun little function we can create. This will take two strings in one order and print them in reverse order. We have the usual call to the main and fmt packages:


Our function then looks like this:


What this does is define x and y as strings, but returns them in reverse order (y, x).

We then create a new function that calls swap() and initializes a and b as two different values inputted by the user, but when called, it reverses the values. This function looks like this:


Our entire program looks like this:


Save that as swap.go and run it:


The app will ask you to type your first and last name. Hit the Enter key and it will print out those two names in reverse.

And that’s how you build your first function in Go. You’ll find both prebuilt and user-defined functions are a necessity for the Go programming language.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.