Mahdi.Kh
September 21, 2026
In this part, we get familiar with variables and their types, and learn how to define a variable and use it correctly.
In this part, we'll cover the following:
A variable is an identifier, or name, that points to a memory address. This address holds a value that can change while the program is running. Variables are used to temporarily hold data while a program runs.
In Go, every variable has a type, and based on that type we can store the data we want in the variable. For example, if a variable's type is numeric, it lets us store numeric values, and we can't store text data in a numeric-type variable.
In Go, we can define variables and assign them values in two ways.
Also, with a single use of var, we can define several variables at once:
After defining variables, we can use them in the program by referring to their names.
We can also change variables' values throughout the program:
In the Go programming language, variables can have different types, each used to store a specific kind of data.
A data type specifies two things:
How much memory it uses
The kind of data that will be stored
In Go, data can be divided into 3 categories:
For now, just keep a general overview of the data types in mind; in later parts, we'll look at each data type.
Literals are fixed, definite expressions that represent a specific kind of data. In programming, literals let you set initial values for variables.
In Go, the following 4 kinds of literals are very common:
Integers: an integer is a sequence of digits. Integer literals are base 10 by default, but various prefixes are used to indicate other bases: 0b for binary (base 2), 0o for octal (base 8), or 0x for hexadecimal (base 16). You can use uppercase or lowercase letters for the prefix. A 0 prefix with no letter after it is another way of showing a number in base 8. Don't use it, since it's quite confusing.
Floats: floating-point numbers are a sequence of digits along with a fractional part.
Runes: in Go, we use runes to represent characters. Each character must be enclosed in single quotes ('). To represent a character, we can use the character itself, or its octal, hex, or Unicode form:
Strings: there are two ways to represent text, or a string. In most cases, you should use double quotes (") to represent strings. The other way is to use backticks (`).
When you use double quotes ("), your text content is interpreted by Go. For example, the \n character causes the rest of the content to be shown on a new line.
Using backticks (`), exactly what you wrote is shown in the output.
In Go, the type of the values you've placed in a variable doesn't change on its own (unintentionally) unless you change it yourself. This concept is called being type safe.
First example: in Go, you can't perform an operation on variables that aren't of the same type.
In the example above, we added an integer and a float and displayed the result. This isn't possible in Go, and the code above will fail at compile time. To fix the error, we adjust the example above as follows:
In the example above, we first converted one variable's type to the type we wanted (type conversion), and then performed the arithmetic operation.
A case where a data type is converted to another type implicitly (on its own) is called implicit type conversion.
Second example: in Go, types are never implicitly converted or interpreted as another type, and if you need a type conversion, you have to do it explicitly, as in the previous example.
In programming languages that aren't type safe, the code above would work correctly! But in Go, your code will fail at compile time.
In Go, implicit interpretation or conversion won't happen, and you need to declare the variable's type explicitly (explicit conversion).
So we can fix the code above as follows:
In this part, we got familiar with variables and their types. We also looked at the concept of literals and being type safe.
Previous part: The Structure of Go Programs | Golang basics
Next part: Constants | Golang basics