Grokking Go
Vote

0% completed

​

Variables in Go

What is a Variable?

  1. Declaring Variables Without Initialization

Syntax

Example

  1. Declaring and Initializing Variables

Syntax

Example

  1. 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.
Image

Example

Go
Go

. . . .
  • var x int → Declares a variable named x of type int. Since no value is provided, x gets 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

Go
Go

. . . .
  • var y int = 10 → Declares a variable y of type int and initializes it with the value 10.
  • var message = "Hello, Go!" → Declares a variable message without specifying the type. Go infers that message is a string because 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 newValue must match the variable's type. If a variable is declared as int, you cannot assign a string to it.

Example

Go
Go

. . . .
  • var count int = 5 → Declares a variable count of type int and sets its initial value to 5.
  • count = 15 → Assigns a new value, 15, to count.

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%


Vote for new content

On This Page

What is a Variable?

  1. Declaring Variables Without Initialization

Syntax

Example

  1. Declaring and Initializing Variables

Syntax

Example

  1. Updating Variable Values

Syntax

Example