Home
Sign in Buy now

Golang Language Fundamentals

Variables, constants and functions

Completed

Summary

In this episode, we explore Go's strong typing system and how it handles variables through zero values instead of null or empty concepts. We learn about variable declarations, the difference between mutable variables and constants, and when to use the shorthand notation within functions. The episode also covers how to create and work with functions in Go, including how to accept arguments and return multiple values from a single function.

Transcript

Go is a strongly typed language, so you must declare what data type you're going to be using. This means that in variables, functions, structs, all of which we'll touch upon very soon, you need to specify if something is a string or an integer or a float, etc. The concept of empty or null does not exist in Go. Instead, it uses what is known as a zero value. If we were to create, say, a string variable, but do not explicitly assign it a value, it would have a zero value. Right, so if we create a variable here called API key and say it's a string, this will now be equal to an empty string. If we were to instead go down here and say API key 2 and say it's equal to super secret, Go will now infer that this is a string. This is a string type based on the content of this variable, and this is also what is known as a short variable declaration. If we go back up here and say var rate limit and set it to an integer, this will now be equal to zero since zero is the zero value of an integer. So what's the difference between specifying a variable like this and specifying it like this? Well, this shorthand notation must be used within a function, so we couldn't say copy this and put it in here. That would not work. We will need to move this inside a function for this to work. Also, all variables in Go are mutable, which simply means that we can change the value. So if we go down here and say API key 2 and now say not super secret. Oh, I can't spell secret. We have overwritten the original value of this variable and notice here that we don't use the colon equal sign. We just use the equal sign. If we wanted to have something that would stay consistent throughout the entire lifetime of our program, we will need to use what is known as a const, and to create a const, we use the const keyword. And let's call this one API key constant, and super secret. Now, this value will be constant throughout the lifetime of our program. The catch is that constant can only hold basic values that Go knows how much memory to allocate to at compile time. So this means like strings, numbers, booleans, etc. You cannot make a constant from something that gets calculated while the program runs. So we now know how to store data for later use with variables and with constants. But what if you want to do something with that data? Well, Go, of course, has support for functions and a function in Go looks something like this. So we specify the func keyword, we give it a name. Let's just say my function and then we can accept some arguments and we can also return some data from the function. Now, if we were to say we wanted to calculate the rate limit or we want to figure out if something has hit a rate limit, we could create a function here called func has hit limit and then we will accept an argument. Let's say usage as an integer and then we will return a boolean and then in the function body, we would have some logic here for calculating rate limit. And then let's just say we return true always for now. Now, we can also accept, of course, multiple arguments and we can return multiple values. So let's say with our has hit limit function here, we wanted to return the remaining usage that was left with whatever program we were running here. So let's say we return an integer along with our boolean and let's just return false and then say 200. So now whenever we call this function, we get back a boolean and an integer. And the way that we would call this function is very similar to how you do it in a lot of other languages. We go down here and then we say hit limit and remaining and then has hit limit and then we pass it some usage. Notice here that we also have these two variables now hit limit and remaining that gets the data type from the return data types of that function.

Episode Notes

Get the Full Course

Unlock all episodes and get lifetime access to course updates.

What others say

"With this course, I'm learning Golang + datastar while building my personal blog hitting several birds with one stone. It's a fantastic course."

Testimonial from ignacio
Ignacio Barceló
Bought Early Access

Video Completed!

Great job finishing this episode

Up Next

Golang Language Fundamentals
If statements and switch statements