Understanding Variable Declarations and Constants in Go

Understanding Variable Declarations and Constants in Go

ยท

3 min read

In the world of Go programming, understanding how to declare variables and constants is fundamental. Let's dive into the syntax and nuances of variable declaration and explore the realm of constants.

Variable Declaration

In Go, variables can be declared in various ways, providing flexibility and clarity to your code. Let's explore some methods:

package main

import "fmt"

func main() {
    // Explicit declaration
    var username string = "hitesh"
    fmt.Println(username)
    fmt.Printf("Variable is of type: %T \n", username) // Similar to C lang

    // Boolean declaration
    var isLoggedIn bool = false
    fmt.Println(isLoggedIn)
    fmt.Printf("Variable is of type: %T \n", isLoggedIn)

    // Unsigned integer declaration
    var smallVal uint8 = 255
    fmt.Println(smallVal)
    fmt.Printf("Variable is of type: %T \n", smallVal)

    // Float declaration
    var smallFloat float64 = 255.45544511254451885
    fmt.Println(smallFloat)
    fmt.Printf("Variable is of type: %T \n", smallFloat)
}
  • In Go, the %T specifier is used to denote the type of a variable.

Implicit Variable Declaration

Go provides implicit variable declaration, where the type is inferred from the initial value assigned to the variable. However, the type cannot be changed later in the program.

package main

import "fmt"

func main() {
    // Implicit type
    var website = "learncodeonline.in"
    fmt.Println(website)
    fmt.Printf("Variable is of type: %T \n", LoginToken)
}
  • Implicit declaration requires that the variable be initialized at the time of declaration.

Default Values of Variables

In Go, uninitialized variables are not assigned garbage values. Instead, they are assigned default values based on their types.

package main

import "fmt"

func main() {
    // Default values and aliases
    var anotherVariable int
    fmt.Println(anotherVariable)
    fmt.Printf("Variable is of type: %T \n", anotherVariable)

    var name string
    fmt.Println(name)
    fmt.Printf("Variable is of type: %T \n", name)
}
  • Different types have different default initial values. For instance, integers have an initial value of 0, while strings have an initial value of "".

No var Style

In Go, we can declare variables without using the var keyword by employing the Walrus operator (:=). However, there are some rules to follow:

  • Variables must be initialized at the time of declaration.

  • The type cannot be explicitly declared.

  • Walrus operator cannot be used for global variables, only within a method.

package main

import "fmt"

func main() {
    // No var style
    numberOfUser := 300000.0
    fmt.Println(numberOfUser)

    // numberOfUser int := 3000 // This syntax is incorrect
}

Constants

Constants provide a way to define values that cannot be changed during the execution of the program. Let's understand their declaration:

Declaration:

Use the const keyword followed by the name and value (optionally with a type declaration).

Example:

const PI = 3.14159  // Untyped float constant
const MAX_USERS int = 100  // Typed integer constant

Understanding variable declarations and constants is crucial for writing clean and efficient Go code. Mastering these concepts will pave the way for building robust applications.

ย