English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go var Keyword

The var keywordInGolangUsed to createVariableSpecific types with appropriate names and initial values. When usingvarWhen declaring a variable with the keyword, initialization is optional, and we will discuss it later in this article.

Syntax:

var identifier type = expression
//Here w3codebox1It is an identifier
//Also known as a variable name, it is of int type and is assigned a value200
var w3codebox1 int = 200

As you know, Go is a statically typed language, but it still provides a feature to omit the data type declaration when declaring variables, as shown in the following syntax. This is usually calledType inference.

Syntax:

var identifier = initialValue
var w3codebox1 = 200

Declare multiple variables using the var keyword

The var keyword is also used to declare multiple variables in a single line. You can also provide initial values for the variables, as shown below:

  • Declare multiple variables and types using the var keyword:

    var w3codebox1, w3codebox2, w3codebox3, w3codebox4 int
  • Declare multiple variables, types, and initial values using the var keyword:

    var w3codebox1, w3codebox2, w3codebox3, w3codebox4 int = 10, 20, 30, 40

Note:

  • You can also useType inference (discussed above), theInferenceThis allows the compiler to know the type, that is, you can remove the type when declaring multiple variables at the same time.

    var w3codebox1, w3codebox2, w3codebox3, w3codebox4 = 10, 20, 30.30, true
  • You can also use the var keyword to declare and initialize different types of values over multiple lines, as shown below:

    var(
         w3codebox1 = 100
         w3codebox2 = 200.57
         w3codebox3 bool
         w3codebox4 string = "www.oldtoolbag.com"
    )
  • When declaring variables, you are only allowed to declare multiple variables of the same type when using the type. However, when you remove the type during declaration, you can declare multiple variables of different types.

    //Declare variables using the var keyword
    package main
    import "fmt"
    func main() {
        //Declare multiple variables of the same type and initialize
        //Declare multiple variables of the same type and initialize with the type on a single line
        var w3codebox1, w3codebox2, w3codebox3 int = 232, 784, 854
        //Declare multiple variables of different types and initialize
        //Specify no type on a single line
        var w3codebox4, w3codebox5, w3codebox6 = 100, "GFG", 7896.46
        fmt.Printf("w3codebox1The value is: %d\n, w3codebox1)
        fmt.Printf("w3codebox2The value is: %d\n, w3codebox2)
        fmt.Printf("w3codebox3The value is: %d\n, w3codebox3)
        fmt.Printf("w3codebox4The value is: %d\n, w3codebox4)
        fmt.Printf("w3codebox5The value is: %s\n, w3codebox5)
        fmt.Printf("w3codebox6The value is: %f, w3codebox6)
    }

    Output:

    w3codebox1The value is: 232
    w3codebox2The value is: 784
    w3codebox3The value is: 854
    w3codebox4The value is: 100
    w3codebox5The value is: GFG
    w3codebox6The value is: 7896.460000

  Key points of var keyword:

  • During the declaration of variables with the var keyword, you can omit the type or = expression, but you cannot omit both at the same time. If you do this, the compiler will throw an error...

  • If the expression is removed, by default, the variable will contain the zero value of the number and the representation string "false" for the boolean value, and nil will contain interface and reference types. Therefore, there is no concept of uninitialized variables in Go language.

    // Concept of var keyword
    package main
    import "fmt"
    func main() {
        //Declare variables without initialization
        var w3codebox1 int
        var w3codebox2 string
        var w3codebox3 float64
        var w3codebox4 bool
        //Displaying a zero value variable
        fmt.Printf("w3codebox1The value is: %d\n, w3codebox1)
        fmt.Printf("w3codebox2The value is: %s\n, w3codebox2)
        fmt.Printf("w3codebox3The value is: %f\n, w3codebox3)
        fmt.Printf("w3codebox4The value is: %t, w3codebox4)
    }

    Output:

    w3codebox1The value is: 0
    w3codebox2The value is:
    w3codebox3The value is: 0.000000
    w3codebox4The value is: false