Welcome back my fellow hackers! Today we’re going to be starting a new series based around learning to program in Golang. The reason we’re starting this series is so that we can eventually create our very own C2 framework! I plan on scripting the C2 server using Python, but we’ll be programming the C2 agent in Golang.
In this first article we’ll be covering the very basics of Golang: Imports, console output, basic data types, variables, and basic mathematics. For this first article, we’ll be using the Go Playground, in which you can write and execute code for practice. Once we get these basics out of the way, we’ll learn to actually compile our code into an executable application (in the next article). So, without further delay, let’s begin!
Imports and Console Output
The first thing we’ll cover is imports and console output. You use imports when you want to import the functions from an external package into your current program. Let’s go ahead and see an example:
We can see here that there are two different ways to import packages. The first way is to individually use the import keyword for each of the packages. The other way is to use a single import followed by each package we want to import inside of a set of parenthesis. The latter is more effective when you have multiple packages you’d like to import.
The package we’re importing here, fmt is the package used to give console output. What this means is that the fmt package contains the necessary functions to output messages to the command line interface (it can be used for other things but we’ll get there in due time). Let’s take a look at an example:
In order to run our code, we need to place it within the main function. A function is a set of pre-coded instructions that can be called at any time. We can make many functions within a single program, stretching across multiple packages. But, no matter what functions we call in our program, execution will start in the main function. Hitting the Run button in the bottom right of the Go Playground will yield the following output:
The Println function we called from the imported fmt package stands for Print Line. It simply prints whatever message is given to it into the command line, followed by a newline. That about does it for imports and console output, now let’s move on to basic data types and variables.
Basic Data Types and Variables
Let’s very quickly define what a data type is. It’s fairly self-explanatory, a data type (hereby referred to as a type) is a type of data. There are many, many different types. But for now we’re just going to be covering the basic ones. First we’ll list the different types:
- bool – holds a true of false value.
- string – a string of characters (example: “Hello, Golang!”)
- int – holds an integer value (example: 5)
- float64 – holds a float value (floating decimal) (example: 5.5)
Now that we’ve covered the basic data types, let’s talk about variables before we give an example. A variable is used to store a value of a particular type. A variable can hold a string, integer, float, or anything else you might be able to think of. However, a variable cannot hold different data types. If a variable is declared as a string, it cannot contain an integer and vice versa. Let’s see an example of declaring some variables that hold our mentioned data types:
We can see in the example there are two ways to declare a variable. I personally prefer the short-handed method, but this is up to personal preferences. By hitting the Run button in the Go Playground we can see our output:
Variables will be used a lot in this series. They are the basis of many operations in Golang. Now that we’ve covered variables, let’s use them in the next portion, basic math operations.
Basic Mathematical Operations
Math has its places. It may not be used in every program you write in Golang, but it will inevitably be used somewhere. It is important to understand how to represent these operations in Golang in the event you find yourself needing them. First, we’ll list the basic math operators then we’ll give an example of them in use:
- Addition is represented by the addition symbol (+)
- Subtraction is represented by the subtraction symbol (-)
- Multiplication is represented by an asterisk (*)
- Division is represented by a forward slash (/)
Now that we’ve covered the operators, let’s see an example of them in action:
You’ll notice that we’ve started with a float instead of an integer. This is an important difference and I’ll explain why. If you’re using an integer, when you perform an operation (especially division) that would yield a float, it will be automatically rounded down. For programs that rely and being accurate, this is a problem. There is one saving grace however, performing an operation on a float will always yield a float, even if the result is a whole number. Let’s go ahead and see the output of this program:
Putting it all Together and Wrapping up
That just about does it for this first entry into this series. For a little bit of reinforcement we’ll run through an example utilizing everything we’ve covered here. It will be, in a sense, our first program written in Go. Let’s take a look at the code:
We start by declaring our variable which represents the number of rabbits we currently have, then we print that value. Then we take our variable and perform some math operations on it, adding one and then adding that number back to the previous one. We print the value again and repeat the process once more. Let’s take a look at the output this program yields:
That does it for this first Golang lesson. Next time we’ll be going over the actual installation and compiling process for making actual Golang programs. We’ll also be going over functions and packages and how to make our own. See you then!
Leave a Reply