Build Your First Go Function
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:
|
1 2 3 |
func name() { … } |
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:
|
1 2 3 |
func add(a int, b int) int { return a + b } |
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:
|
1 2 3 4 5 |
} func main() { fmt.Println("The total of a + b equals", add(42, 13)) } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
package main import "fmt" func add(a int, b int) int { return a + b } func main() { fmt.Println("The total of a + b equals", add(42, 13)) } |
Save that file as function.go and run it with the command:
|
1 |
go run function.go |
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:
|
1 2 3 4 5 6 7 |
package main import "fmt" func add(a int, b int) int { return a + b } |
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:
|
1 2 |
var num1 int var num2 int |
Our first Print/Scan combo is for num1, and looks like this:
|
1 2 |
fmt.Print("Enter a number for a: ") fmt.Scan(&num1) |
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:
|
1 2 |
fmt.Print("Enter a number for b: ") fmt.Scan(&num2) |
We then print a final line and then call our add() function with the following:
|
1 |
fmt.Println("The total of a + b equals", add(num1, num2)) |
The entire app looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package main import "fmt" func add(a int, b int) int { return a + b } func main() { var num1 int var num2 int fmt.Print("Enter a number for a: ") fmt.Scan(&num1) fmt.Print("Enter a number for b: ") fmt.Scan(&num2) fmt.Println("The total of a + b equals", add(num1, num2)) } |
Save that file as input.go and run it with this command:
|
1 |
go run input.go |
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:
|
1 2 3 4 5 6 7 8 9 |
func main() { var num1 int var num2 int fmt.Print("Enter a number for a and one for b: ") fmt.Scan(&num1, &num2) fmt.Println("The total of a + b equals", add(num1, num2)) } |
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:
|
1 2 3 |
package main import "fmt" |
Our function then looks like this:
|
1 2 3 |
func swap(x, y string) (string, string) { return y, x } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
func main() { var name1 string var name2 string fmt.Print("Type your first and last names") fmt.Scan(&name1, &name2) a, b := swap(name1, name2) fmt.Println(a, b) } |
Our entire program looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package main import "fmt" func swap(x, y string) (string, string) { return y, x } func main() { var name1 string var name2 string fmt.Print("Type your first and last names ") fmt.Scan(&name1, &name2) a, b := swap(name1, name2) fmt.Println(a, b) } |
Save that as swap.go and run it:
|
1 |
go run swap.go |
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.