Go Variables
Variables are fundamental building blocks in Go. They act as containers for
storing data values that can be used and manipulated throughout your program.
Understanding Variable Types in Go
Go is a statically typed language, which means every variable has a defined
type. Some of the most commonly used types include:
- int – Stores whole numbers (e.g., 10, -5)
- float32 / float64 – Stores decimal numbers (e.g., 3.14, -0.99)
- string – Stores text enclosed in double quotes (e.g., "Go Language")
- bool – Stores boolean values: true or false
More advanced data types are covered in the Go Data Types section.
Declaring Variables in Go
Go provides two primary ways to declare variables:
1. Using the var Keyword
You can explicitly declare a variable using the var keyword, optionally
specifying its type and value.
Syntax
var variableName type = value
Example
2. Using Short Declaration (:=)
Go also supports a shorter syntax using :=, where the type is automatically
inferred from the assigned value.
Syntax
variableName := value
Example
Note: The := syntax can only be used inside functions and requires
immediate initialization.
Variable Declaration with Initial Values
You can declare and initialize variables in a single line.
Variable Declaration Without Initial Values
In Go, all variables are automatically initialized with default (zero)
values if no value is assigned.
Default Values
- string → "" (empty string)
- int → 0
- bool → false
Example
Assigning Values After Declaration
You can assign values to variables after declaring them, which is useful
when the value is not immediately known.
Difference Between var and :=
Example: Global and Local Variables
The var keyword allows you to declare variables outside functions (global
scope).
Go Multiple Variable Declaration
In Go, you can declare multiple variables in a single statement. This helps
reduce repetitive code and improves readability, especially when working
with related data.
Declaring Multiple Variables on One Line
You can declare several variables of the same type in one line by specifying
the type once and assigning values in order.
Example:
How it works:
- x, y, and z are all declared as int
- Values are assigned in sequence: x=10, y=20, z=30
Declaring Variables with Different Types
If you omit the explicit type, Go automatically infers the type based on the
assigned value. This allows you to declare variables of different types in a
single line.
Example:
Key Points:
Go uses type inference when the type is not explicitly declared
The := shorthand can only be used inside functions
Declaring Variables in a Block
For better organization and readability, especially when declaring multiple
variables, Go allows grouping them in a block.
Example:
Why use a block declaration?
- Improves readability for large sets of variables
- Keeps related variables organized
- Makes future modifications easier
Go Variable Naming Rules
Choosing meaningful and valid variable names is essential for writing
clean, readable, and maintainable code in Go. Go enforces a set of
naming rules while also encouraging simple and consistent naming
conventions.
Basic Rules for Naming Variables in Go
When declaring variables in Go, follow these rules:
- Start with a letter or underscore (_) Variable names must begin with a letter (a–z or A–Z) or an underscore.
- Cannot begin with a digitNames like 1value or 9count are invalid.
- Use only alphanumeric characters and underscores Special characters such as @, #, or - are not allowed.
- Case-sensitive identifiers total, Total, and TOTAL are treated as three distinct variables.
- No spaces allowed Use a consistent naming style instead of spaces.
- Avoid reserved keywords You cannot use Go keywords like var, func, or package as variable names.
- No strict length limit However, shorter, meaningful names are preferred for readability.
Example: Valid and Invalid Variable Names
Naming Multi-Word Variables
When variable names contain multiple words, readability becomes
important. Go developers typically follow these widely accepted naming
conventions:
1. Camel Case (Recommended in Go)
The first word is lowercase, and each subsequent word starts with a capital
letter.
✔ Preferred style for most Go variables
✔ Improves readability without extra characters
2. Pascal Case
Each word starts with a capital letter.
var StudentName = "Ravi Kumar"
- Commonly used for exported identifiers (public variables, functions, or types)
3. Snake Case
Words are separated using underscores.
var student_name = "Ravi Kumar"
- Less common in Go, but sometimes used in specific cases (e.g., constants or interoperability with other systems)
AI Code Explanation
Go Language
