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

Basic Tutorial in Golang

Control statements in Golang

Function & Method in Golang

Structure in Golang

Slice & Array in Golang

String (String) in Golang

Pointer in Golang

Interface in Golang

Concurrency in Golang

Error in Golang

Other Miscellaneous in Golang

Go Language Variable

Typical programs use various values, which may change during execution.

For example, a program that performs certain operations on the values entered by the user. A user's input value may be different from another user's input value. Therefore, it is necessary to use variables because other users may not use the same value. When a new value entered by a user is to be used in the process of operation, these values can be temporarily stored in the computer's random access memory. These values differ in the execution of this part of memory, so this term is also calledVariable. A variable is a placeholder for information that can be changed at runtime. And variables allow retrieval and processing of stored information.

VariableNaming rules:

The variable name must start with a letter or an underscore (_). And the name may contain the letter 'a'.-z" or "A"-Z" or the number 0-9as well as the character " _".

Geeks, geeks, _geeks23  //Legal variable
123Geeks, 23geeks      // Illegal variable

Variable names should not start with numbers.

234geeks  //Illegal variable

Variable names are case-sensitive.

geeks and Geeks are two different variables

Keywords are not allowed to be used as variable names.

The length of the variable name has no limit, but it is recommended to use4to15The best length of a letter is

Declare variables

In the Go language, variables are created in two different ways:

(1) Using the var keyword:In the Go language, variables are created using specific typesvarKeywords created, which are associated with the variable name and assigned their initial value.

Syntax:

var variable_name type = expression

Important matters:

In the above syntax,Type (type) or=The expression can be removed, but not both at the same time in the variable declaration.

If the type is removed, the type of the variable is determined by the initialization value of the value in the expression.

//The concept of variables
package main
import "fmt"
func main() {
    //Variable declaration and initialization
    //Explicit type
    var myvariable1 = 20
    var myvariable2 = "w3codebox"
    var myvariable3 = 34.80
    // Display the value and the
    // type of the variables
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable1The type is: %T\n", myvariable1)
    fmt.Printf("myvariable2The value is: %s\n", myvariable2)
    fmt.Printf("myvariable2The type is: %T\n", myvariable2)
    fmt.Printf("myvariable3The value is: %f\n", myvariable3)
    fmt.Printf("myvariable3The type is: %T\n", myvariable3)
}

Output:

myvariable1The value is: 20
myvariable1The type is: int
myvariable2The value is: w3codebox
myvariable2The type is: string
myvariable3The value is: 34.800000
myvariable3The type is : float64

If the expression is removed, the type of the variable is zero, the number is zero, the boolean value is false, and the string is"", interfaces, and reference types are nil. Therefore,There is no concept of uninitialized variables in the Go language.

package main
import "fmt"
func main() {
    //Variable declaration and initialization do not use expressions
    var myvariable1 int
    var myvariable2 string
    var myvariable3 float64
    //Display the variable with a value of 0
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable2The value is: %d\n", myvariable2)
    fmt.Printf("myvariable3The value is: %d\n", myvariable3)
}

Output:

myvariable1The value is: 0
myvariable2The value is: !%d(string=)
myvariable3The value is: !%d(float64=0)

If a type is used, multiple variables of the same type can be declared in a single declaration.

package main
import "fmt"
func main() {
    // Declare and initialize multiple variables of the same type on the same line
    var myvariable1, myvariable2, myvariable3 int = 2, 454, 67
    // Output the value of the variable
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable2The value is: %d\n", myvariable2)
    fmt.Printf("myvariable3The value is: %d\n", myvariable3)
}

Output:

myvariable1The value is: 2
myvariable2The value is: 454
myvariable3The value is: 67

If the type is removed, multiple variables of different types can be declared in a single declaration. The type of the variable is determined by the initialization value.

package main
import "fmt"
func main() {
    //Multiple variables of different types
    //Declare and initialize in a single line
    var myvariable1, myvariable2, myvariable3 = 2, "GFG", 67.56
    // Print variable value and type
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable1The type is: %T\n", myvariable1)
    fmt.Printf("\nmyvariable2The value is: %s\n", myvariable2)
    fmt.Printf("myvariable2The type is: %T\n", myvariable2)
    fmt.Printf("\nmyvariable3The value is: %f\n", myvariable3)
    fmt.Printf("myvariable3The type is: %T\n", myvariable3)
}

Output:

myvariable1The value is: 2
myvariable1The type is: int
myvariable2The value is: GFG
myvariable2The type is: string
myvariable3The value is: 67.560000
myvariable3The type is : float64

Function calls that return multiple values allow you to initialize a set of variables.

For example:

//Here, the os.Open function returns a
//File i variable and an error
//In the j variable
var i, j = os.Open(name)

(2) Using short variable declaration: Using short variable declarationto declare and initialize local variables within a function.

Syntax:

variable_name:= expression

Note:Please do not use:=and=confusion between:= is declaration, and = is assignment.

Important matters:

The type of the variable is determined by the type of the expression in the above expression.

package main
import "fmt"
func main() {
    // Use short variable declarations
    myvariable1 := 39
    myvariable2 := "oldtoolbag.com"
    myvariable3 := 34.67
    // Print variable value and type
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable1The type is: %T\n", myvariable1)
    fmt.Printf("\nmyvariable2The value is: %s\n", myvariable2)
    fmt.Printf("myvariable2The type is: %T\n", myvariable2)
    fmt.Printf("\nmyvariable3The value is: %f\n", myvariable3)
    fmt.Printf("myvariable3The type is: %T\n", myvariable3)
}

Output:

myvariable1The value is: 39
myvariable1The type is: int
myvariable2The value is: oldtoolbag.com
myvariable2The type is: string
myvariable3The value is: 34.670000
myvariable3The type is : float64

Due to their conciseness and flexibility, most local variables are declared and initialized using short variable declarations.

The var declaration of variables is used for local variables that need explicit types different from the initial value assignment expressions, or for those variables whose values are assigned later and whose initial values are not important.

Using short variable declarations, multiple variables can be declared in a single declaration.

package main
import "fmt"
func main() {
    //Declare and initialize variables in a single line
    //Use short variable declarations
    //Multiple variables of the same type
    myvariable1, myvariable2, myvariable3 := 800, 34.7, 56.9
    // Print variable value and type
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable1The type is: %T\n", myvariable1)
    fmt.Printf("\nmyvariable2The value is: %f\n", myvariable2)
    fmt.Printf("myvariable2The type is: %T\n", myvariable2)
    fmt.Printf("\nmyvariable3The value is: %f\n", myvariable3)
    fmt.Printf("myvariable3The type is: %T\n", myvariable3)
}

Output:

myvariable1The value is: 800
myvariable1The type is: int
myvariable2The value is: 34.700000
myvariable2The type is : float64
myvariable3The value is: 56.900000
myvariable3The type is : float64

In short variable declarations, it is allowed to initialize a group of variables with the return values of a function that returns multiple values.

//Here, the os.Open function returns a
//File i variable and an error
//In the j variable
i, j := os.Open(name)

Short variable declarations only act like assignments for those variables that have already been declared in the same syntax block. Variables declared in external blocks will be ignored. As shown in the following example, at least one of these variables is a new variable.

package main
import "fmt"
func main() {
    //Use short variable declarations
    //Here, the short variable declaration action
    //As myvar2Variable assignment
    //Because the same variable exists in the same block
    //Therefore myvar2The value from45Change to100
    myvar1, myvar2 := 39, 45
    myvar3, myvar2 := 45, 100
    //If you try to run commented lines,
    //Then the compiler will give an error because
    //These variables have already been defined, for example
    // myvar1, myvar2:= 43,47
    // myvar2:= 200
    // Print variable value
    fmt.Printf("myvar1 and myvar2 The value: %d %d\n", myvar1, myvar2)
    fmt.Printf("myvar3 and myvar2 The value: %d %d\n", myvar3, myvar2)
}

Output:

myvar1 and myvar2 The value: 39 100
myvar3 and myvar2 The value: 45 100

Using short variable declarations, multiple variables of different types can be declared in a single declaration. The types of these variables are determined by the expressions.

package main
import "fmt"
func main() {
    //Declare and initialize in a single line
    //Use short variable declarations
    //Multiple variables of different types
    myvariable1, myvariable2, myvariable3 := 800, "w3codebox 47.56
    // Print variable value and type
    fmt.Printf("myvariable1The value is: %d\n", myvariable1)
    fmt.Printf("myvariable1The type is: %T\n", myvariable1)
    fmt.Printf("\nmyvariable2The value is: %s\n", myvariable2)
    fmt.Printf("myvariable2The type is: %T\n", myvariable2)
    fmt.Printf("\nmyvariable3The value is: %f\n", myvariable3)
    fmt.Printf("myvariable3The type is: %T\n", myvariable3)
}

Output:

myvariable1The value is: 800
myvariable1The type is: int
myvariable2The value is: w3codebox
myvariable2The type is: string
myvariable3The value is: 47.560000
myvariable3The type is : float64