Tagvariadic functions

Using Functions in Go

Functions are a feature in many different languages. Functions allow us to save pieces of code to reuse at any point in our program. This makes things significantly easier for many tasks. Not only does it decrease the amount that we have to write, it also makes the code more readable. Functions in Go have many specific features that make them particularly useful.

Basic Function Styles in Go

Go supports many different styles of functions. In Go, you can create first-class functions, custom function types, function literals, and functions with multiple return values. You may also use functions as a closure and define high-order functions. We will talk about each of these types of functions one at a time as we work through this tutorial. First, let’s just look at a basic function in Go and how it is structured.

functions in goHere are two simple examples of functions in Go. Our first example just calls the fmt.Println() function inside of it which prints the string “Hello, World” then creates a new line. It takes no arguments and we can simply call it by typing its name followed by a set of parenthesis. Our addition function, allows us to pass two integer values to it. It combines these two values with the addition operator and returns that sum as an integer. 

Notice that we define the types in this function. This serves two main purposes; It allows us to make the function more readable and it makes the function type safe. In this case, our function can only accept integer values and will only return integer values. This removes any unpredictable behavior from the function. Also, we can make this function definition shorter because of its successive type information. Look at the third example for this.

Return Statements in Go and Variadic Functions

We have an explicit return statement in this function. In Go, functions won’t automatically return the last line; we must use the return operator. In this case, we return the sum. We can also use this return operator to return multiple return statements from a function. Let’s look at an example of this.

Go also can make use it’s slice abstraction when we want to pass a non-set amount of arguments to a function. This is what’s called a variadic function. Most of the core functions in Go are variadic, which means that you can pass any number of arguments to them. If we wanted to make a summation function we would use a variadic function to do this.

In this example, we use this triple dot syntax to signify that we can take an arbitrary amount of arguments. It also tells the compiler to make a slice out of this data. For our first execution of sum, we pass nine arguments to it. The function then treats these like a slice of integers. We use the range keyword to iterate through all of the values and we add them all together. We then return a single integer. 

Conclusion

In this post, we took a look at basic functions in Go. We looked at how we can use functions with no arguments or returns, functions that have arguments and/or returns and functions with multiple returns. We also looked at variadic functions in Go. In our next few posts, we will look at recursion and some of the other types of functions in Go. 

Manipulating Strings in Go

Strings are a pretty standard primitive type in most programming languages. They give the programmer the ability to output messages and store data. Today, we are going to look at strings and string manipulation in Go. We are also going to take a look at some of the print and input functions in Go.

Defining Strings in Go

Strings in Go are defined using double quotes "". You can’t use single quotes '' in Go because this is reserved for single characters or runes. A string by definition is a collection of characters surrounded by double quotes. In Go, we can do many cool things with strings such as regex, data storage, output, debugging, etc.

hello-world-go

You may have noticed that in our Hello world program from the last post we imported a library called fmt. The fmt library gives us access to I/O in Go. This library gives us access to different print functions as well as scanner functions. We can use these print functions to print out to our terminal/console. We can also use the scanner functions to take in input from the command line.

Using I/O for Input and Output

The program above allows us to take in an input and then output "Hello, Input!" to the screen. In the program, we are using two different print functions and a scanner function. We are also making use of a reference and a string variable.

The fmt.Println() function will print the string or arguments that are in the parenthesis out then automatically create a new line. We could also get this behavior by changing that function to fmt.Print("\n"). The \n character set is what is called a newline character. The backslash is the escape character and the “n” is what tells it to create a new line. The fmt.Scanln() function lets us scan the input until the user hits the enter key or creates a new line.

Formatting Strings in Go

The fmt.Printf() function stands for print format. It allows us to format the string inside of the function. You may have noticed the use of a %s pair of characters. This is telling the string that we want to format that area of the string by inputting the value of a string variable, in this case, input. There are many other two letter codes that we can use to format strings in Go.

strings-in-go

As you can see above, there are many different ways to format strings and the values being passed into those strings via the fmt.printf() function. All of the print functions in the fmt library are called variadic functions. Variadic functions in Go let the user input as many arguments into them as they want. We will talk about this more later but for now it is worth mentioning at least.

A Brief Word on References and Pointers

There is at least one more thing worth looking at from this piece of code which is the line: fmt.Scanln(&input). The reason this line is so important to look at is because we are using something called a reference in place of the normal variable input. In many low-level programming languages, we have something called a pointer and something else called a reference. The and & sign right before the input variable inside of the Scanln() function indicates that we are referencing the variable called input. The & operator finds the address of a variable and we assign the input to this variable. We will talk further about references when we get to pointers later down the line.

Conclusion

In this post, we looked at strings in Go. We also looked at how we can manipulate these strings and we looked at the basic I/O library in Go. We briefly talked about variadic functions, references and pointers as well. In our next posts, we will talk about logical operators, Loops and basic control flow in Go.