Go Programming: Variables and Data Types
Declaring Variables
In Go, variables are declared using the var
keyword. The general syntax for declaring a variable looks like this:
var variableName type
Here, variableName
is the name of the variable, and type
specifies the type of data the variable will hold.
You can also declare a variable and initialize it at the same time:
var variableName type = value
This allows you to assign a value to the variable upon creation.
Example:
package main
import "fmt"
func main() {
var age int = 25
var name string = "John Doe"
fmt.Println("Name:", name)
fmt.Println("Age:", age)
}
Explanation:
var age int = 25
: Declares a variableage
of typeint
and assigns it the value25
.var name string = "John Doe"
: Declares a variablename
of typestring
and assigns it the value"John Doe"
.fmt.Println
: Prints the values ofname
andage
to the console.
Short Variable Declaration
Go allows a shorthand for variable declaration inside functions using the :=
syntax. This syntax automatically infers the type based on the value assigned.
name := "John Doe"
age := 25
Explanation: In this case, name
is inferred to be of type string
, and age
is inferred to be of type int
based on the values assigned.
Data Types in Go
Go provides several built-in data types. Let’s go through the most commonly used ones:
1. Integers
Go has various integer types depending on the size of the number:
int
(platform-dependent, typically 32 or 64 bits)int8
,int16
,int32
,int64
(fixed-size signed integers)uint
(unsigned integers, no negative values)uint8
,uint16
,uint32
,uint64
(fixed-size unsigned integers)
Example:
var x int = 10
var y int64 = 1000
2. Floating Point Numbers
Go supports two floating point types:
float32
(single-precision)float64
(double-precision)
Example:
var pi float64 = 3.14159
Explanation: The float64
type is used to store decimal values with higher precision.
3. Strings
Go has a built-in string
type for storing text. Strings are immutable in Go, which means their values cannot be changed once they are created.
Example:
var greeting string = "Hello, Go!"
4. Booleans
A bool
type holds one of two values: true
or false
. It is commonly used for conditional logic.
Example:
var isGoAwesome bool = true
Zero Values
In Go, variables are initialized with default values when they are declared but not assigned a value. These default values are known as zero values. Here are the zero values for common data types:
int
→0
float64
→0.0
string
→""
(empty string)bool
→false
Example:
package main
import "fmt"
func main() {
var age int
var isValid bool
var name string
fmt.Println("Age:", age) // Output: Age: 0
fmt.Println("Is Valid:", isValid) // Output: Is Valid: false
fmt.Println("Name:", name) // Output: Name:
}
Constants
In Go, constants are variables whose values cannot be changed after they are assigned. You can declare constants using the const
keyword.
Example:
const pi = 3.14159
const maxLimit = 100
Explanation: The pi
constant is set to 3.14159
and cannot be modified later, and the maxLimit
constant is set to 100
and is immutable.
Summary
- Variables: Declared using the
var
keyword, and can be initialized at the same time. - Short declaration (
:=
): Automatically infers the type. - Data types: Common data types in Go include
int
,float64
,string
, andbool
. - Zero values: Variables without an initial value are assigned default values (zero values).
- Constants: Declared with
const
, and their values are immutable.
In the next post, we will explore Control Flow in Go, including if
, else
, and switch
statements.