Constants & Variables


Constants & Variables

In the Go programming language, constants and variables are fundamental elements used to store and manage data. Below is a detailed explanation of both concepts, including syntax and examples.


1. Constants in Go

Definition

Constants are fixed values that cannot be modified once assigned. They are useful for storing unchanging data such as mathematical values, configuration settings, and predefined limits.

Syntax

In Go, the const keyword is used to declare a constant. The general syntax is:

const constantName dataType = value

However, Go allows implicit typing, so specifying the data type is optional.

Example

package main

import "fmt"

func main() {
    const pi float64 = 3.14159 // Explicit type declaration
    const gravity = 9.8        // Implicit type assignment

    fmt.Println("Value of Pi:", pi)
    fmt.Println("Acceleration due to Gravity:", gravity)
}

Key Points About Constants

  • Constants must be assigned at compile-time and cannot be modified during execution.
  • Only basic data types such as int, float, string, and bool can be used as constants.
  • Constants can also be defined in groups using parentheses:
      const (
          Pi       = 3.14
          Language = "Go"
      )

2. Variables in Go

Definition

Variables are used to store data that can change during the execution of a program. Unlike constants, variables can be reassigned new values.

Syntax

There are multiple ways to declare variables in Go:

  • Using the var Keyword (Explicit Type Declaration)
      var variableName dataType = value
  • Type Inference (Implicit Type Declaration)
      var variableName = value
  • Short Variable Declaration (:= Operator, Used Inside Functions Only)
      variableName := value
    • Example
    • package main
      
      import "fmt"
      
      func main() {
          // Explicit declaration
          var name string = "Alice"
          var age int = 25
      
          // Implicit declaration
          var country = "USA"
          var isEmployed = true
      
          // Short declaration
          salary := 50000.50
      
          // Printing variables
          fmt.Println("Name:", name)
          fmt.Println("Age:", age)
          fmt.Println("Country:", country)
          fmt.Println("Employed:", isEmployed)
          fmt.Println("Salary:", salary)
      }

Key Points About Variables

  • Global variables can be declared outside functions but must use the var keyword.
  • The short declaration (:=) method can only be used inside functions.
  • Unused variables in Go lead to compilation errors, promoting clean and efficient code.

3. Multiple Variable Declaration

Go allows multiple variables to be declared at once using a single var statement or the short declaration syntax.

Example

package main

import "fmt"

func main() {
    // Declaring multiple variables at once
    var a, b, c int = 10, 20, 30
    x, y, z := "Go", 3.14, false

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

4. Constant vs. Variable: Key Differences

Feature Constants (const) Variables (var)
Mutability Immutable (cannot change) Mutable (can be reassigned)
Scope Local or global Local or global
Assignment Must be initialized at declaration Can be assigned later
Data Types Basic types only All data types allowed
Usage Used for fixed values like Pi Used for changing data

Conclusion

  • Use constants when the value remains the same throughout the program.
  • Use variables when the data may change during execution.
  • Go promotes type inference, reducing redundancy in declarations.

By understanding constants and variables, you can write cleaner, more efficient, and maintainable Go programs.

PreviousNext