Basics of VariablesBasics of Variables

Variables represent one of the most crucial concepts in any programming language. They essentially serve as named memory locations that allow us to access and modify the data stored at those memory locations.

With variables, we can store various types of data in memory, including numbers and text. We will delve into data types in the next section. For now, let's focus on declaring and initializing variables without specifying their types.

The var keyword is employed to declare a variable, followed by the variable's name (myVar) and an equal sign (=) to assign the value on the right (10) to the variable. Another example for better understanding is:



Another way to achieve the same result is by employing the := operator, and in this scenario, there's no need to utilize the var keyword for variable declaration:



Naming Variables

Variables can be given any name, although there are specific rules for declaring variables:

  • A variable's name must start with a letter or an underscore _ character.
  • Variable names are case-sensitive, so myVar and MyVar are distinct variables.
  • A variable cannot have a reserved word as its name. In Go, words like var, for, if, and func, among others, are reserved for special purposes and cannot be used as variable names.

Everything was clear?

Section 1. Chapter 4