Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Variables in GoLang

Variables in GoLang

Variables in GoLang

In Go, variables are used to store data that can be manipulated during the execution of a program. Go is a statically typed language, meaning that every variable must have a declared type, and its type cannot be changed once set. Go provides various ways to declare and initialize variables.

Declaring and Initializing Variables

  1. Basic Declaration:Variables can be declared using the var keyword, followed by the variable name and the type.

    var name string // Declare a string variable named 'name'

  2. Declaration with Initialization:You can also initialize a variable when declaring it.

    var age int = 30 // Declare and initialize 'age' with a value of 30

    var name string = "Alice" // Declare and initialize 'name' with "Alice"

  3. Type Inference:Go supports type inference, meaning that if you initialize a variable, Go can automatically infer its type. You don't need to specify the type explicitly.

    var age = 30 // Go infers that 'age' is of type int

    var name = "Alice" // Go infers that 'name' is of type string

  4. Short Declaration (:=):Go also allows short variable declaration within functions, where you don't need to explicitly mention the type. The type is inferred based on the assigned value.

    age := 30 // Go infers 'age' is of type int

    name := "Alice" // Go infers 'name' is of type string

    This shorthand syntax is only allowed inside functions and is very commonly used in Go.

Types of Variables in Go

  1. Basic Types:

    • int, int32, int64 (signed integer types)
    • uint, uint32, uint64 (unsigned integer types)
    • float32, float64
    • bool (boolean values)
    • string (text data)
  2. Arrays:Go supports arrays that can store multiple values of the same type.

    var arr [3]int // Array of 3 integers

  3. Slices:Slices are more flexible than arrays and allow dynamic resizing.

    var slice []int // Slice of integers

  4. Structs:Go supports structs, which are collections of fields that can have different types.

    type Person struct {

    Name string Age int}var person Person // Declare a variable of type 'Person'

  5. Maps:Maps are key-value pairs, like dictionaries in Python or hash maps in other languages.

    var studentGrades map[string]int // Map of string to int

  6. Pointers:Go has pointers, which store the memory address of a variable.

    var ptr *int // Pointer to an integer

Zero Value

In Go, variables are initialized to zero values if no initial value is assigned. This applies to all types:

  • For numeric types (int, float64), the zero value is 0.
  • For bool, the zero value is false.
  • For string, the zero value is "" (empty string).
  • For arrays, slices, maps, and structs, the zero value is a type-specific default (e.g., an empty slice or map).

var a int // Zero value is 0

var b bool // Zero value is falsevar c string // Zero value is ""

Global Variables

Variables declared outside of functions are global and can be accessed anywhere within the package.

package main

import "fmt"var globalVar = "I'm global"func main() { fmt.Println(globalVar) const Pi = 3.14 // Declare a constant 'Pi' with the value 3.14

Multiple Variable Declaration

Go allows you to declare multiple variables in a single statement, separated by commas.

var x, y, z int // Declare multiple variables of the same type

x, y, z = 10, 20, 30 // Assign values to multiple variables

You can also use short declaration for multiple variables:

a, b, c := 10, 20, 30

Example Code:

package main

import "fmt"func main() { // Declaring and initializing variables var name string = "Alice" var age int = 30 height := 5.6 // Type inferred as float64 isStudent := false // Type inferred as bool // Outputting the values of variables fmt.Println("Name:", name) fmt.Println("Age:", age) fmt.Println("Height:", height) fmt.Println("Is a student:", isStudent) // Modifying a variable's value age = 31 fmt.Println("New age:", age)}

Summary

  • Variables in Go are declared using the var keyword or shorthand :=.
  • Zero values are automatically assigned to variables that are declared but not initialized.
  • Go supports basic types, arrays, slices, structs, maps, pointers, and constants.
  • Go has strong and static typing, meaning every variable's type must be declared or inferred explicitly.
  • Short declaration (:=) is commonly used inside functions for quick assignments.

Go's simplicity and explicitness in variable declaration help reduce errors in programs and make the code easier to maintain.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql