GO Tutorial



VARIABLES IN GO


πŸ“¦ What Are Variables?

Variables are containers that store data values. In Go, you declare variables with the var keyword and optionally initialize them. Golang is statically typed, meaning you must specify or infer the data type.

βœ… 1. Basic Variable Declaration

package main

import "fmt"

func main() {
    var name string = "Technorank"
    var age int = 25
    fmt.Println("Name:", name)
    fmt.Println("Age:", age)
}
  

Output:

Name: Technorank
Age: 25
  

⚑ 2. Declare Without Initializing

If you don’t assign a value at the time of declaration, Go assigns a zero value (like 0, "", or false).

var count int  // count will be 0 by default
fmt.Println(count)
  

⚑ 3. Short Variable Declaration (Inside Functions)

You can use := to declare and assign in one step, only inside functions.

message := "Hello Go!"
fmt.Println(message)
  
πŸ”Ž Rule: Use := only inside functions. Outside functions, you must use var.

πŸ”’ 4. Multiple Variable Declaration

var a, b, c = 1, 2, 3
x, y := "Go", true

fmt.Println(a, b, c)
fmt.Println(x, y)
  

🎯 5. Variable Types in Go

  • int – for integers
  • float64 – for floating numbers
  • string – for text
  • bool – for true/false values

🧠 6. Constants vs Variables

Variables can change. Constants (const) can't.

const pi = 3.14
fmt.Println(pi)
  
πŸ“Œ Summary:
  • Use var to declare variables with or without initialization.
  • Use := for short declarations inside functions.
  • Each variable has a typeβ€”explicit or inferred.
  • Variables hold values that your program works with.

πŸ’» Practice Online

Try the examples on Go Playground


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review