0% completed
Variables in Go
On This Page
What is a Variable?
- Declaring Variables Without Initialization
Syntax
Example
- Declaring and Initializing Variables
Syntax
Example
- Updating Variable Values
Syntax
Example
In this lesson, we will learn how to declare variables in Go. Variables are used to store data that can be used and updated later in your program. We will see how to declare, initialize, and update variable values using clear examples and simple code.
What is a Variable?
- A variable is a named space in memory.
- It holds data that can be changed during the execution of a program.
- Variables help store values such as numbers, text, and more.
1. Declaring Variables Without Initialization
At times, you may want to create a variable without setting its value immediately. In Go, if you declare a variable without providing a value, it is automatically given a default value based on its type. For example, an integer variable will have the default value 0, a boolean will default to false, and a string will be an empty string "".
Syntax
var variableName dataType
var→ The keyword used to declare a variable.variableName→ The name of the variable (must be unique in the same scope).dataType→ Specifies the type of data the variable will hold (e.g.,int,float64,string). We will learn more about data types in upcoming lessons.
Example
var x int→ Declares a variable namedxof typeint. Since no value is provided,xgets its default value,0.
2. Declaring and Initializing Variables
You can declare a variable and assign it a value in a single line. This is useful when you know the initial value the variable should have. Go also supports type inference, which means that if you provide a value, Go can determine the type automatically.
Syntax
With Explicit Type Declaration
var variableName dataType = value
With Type Inference (Let Go Determine the Type)
var variableName = value
var→ Used to declare a variable.variableName→ The name of the variable.dataType→ The type of the variable (e.g.,int,float64,string).= value→ Assigns a value to the variable.- If type inference is used, Go automatically detects the type from the assigned value.
Example
var y int = 10→ Declares a variableyof typeintand initializes it with the value10.var message = "Hello, Go!"→ Declares a variablemessagewithout specifying the type. Go infers thatmessageis astringbecause the assigned value is text.
3. Updating Variable Values
Once a variable is declared, you can change its value later in the program. This is useful when the data needs to be updated based on conditions or user input.
Syntax
variableName = newValue
variableName→ The name of the variable to be updated.=→ The assignment operator, used to assign a new value.newValue→ The new value to be stored in the variable.
⚠ Note: The type of
newValuemust match the variable's type. If a variable is declared asint, you cannot assign astringto it.
Example
var count int = 5→ Declares a variablecountof typeintand sets its initial value to5.count = 15→ Assigns a new value,15, tocount.
This lesson covered the basics of declaring, initializing, and updating variables in Go. In the next lesson, we will dive deeper into data types in Go to understand what kind of values variables can hold.
Reading Progress
0%
On This Page
What is a Variable?
- Declaring Variables Without Initialization
Syntax
Example
- Declaring and Initializing Variables
Syntax
Example
- Updating Variable Values
Syntax
Example