Variables

This Go code demonstrates variable declaration and initialization using different methods. Let's go through each part of the code:

package main

import "fmt"

func main() {
    // Declare and initialize a variable 'a' with the string value "initial"
    var a = "initial"
    fmt.Println(a)

    // Declare and initialize two variables 'b' and 'c' with integer values 1 and 2
    var b, c int = 1, 2
    fmt.Println(b, c)

    // Declare and initialize a variable 'd' with a boolean value true
    var d = true
    fmt.Println(d)

    // Declare a variable 'e' without initializing it, defaults to the zero value for its type (int in this case)
    var e int
    fmt.Println(e)

    // Short declaration and initialization of a variable 'f' with the string value "apple"
    f := "apple"
    fmt.Println(f)
}

Output

initial
1 2
true
0
apple

Explanation:

  1. var a = "initial": Declares a variable a of type string and initializes it with the value "initial". The type is inferred from the assigned value.

  2. var b, c int = 1, 2: Declares two variables, b and c, both of type int. They are initialized with the values 1 and 2, respectively.

  3. var d = true: Declares a variable d of type bool and initializes it with the value true.

  4. var e int: Declares a variable e of type int without initializing it. In Go, variables that are declared without an explicit initialization are given a zero value for their type. In this case, e will have the zero value for an int, which is 0.

  5. f := "apple": Uses the short declaration syntax to declare and initialize a variable f with the string value "apple". The type is inferred from the assigned value.

The fmt.Println statements are used to print the values of these variables to the console when the program is executed.

Last updated