go tutorial series - variables

Go language is a static type language, so variables have explicit types, and the compiler will check the correctness of variable types. In mathematics, a variable represents a number that has no fixed value and can be changed. But from the perspective of computer system implementation, variables are one or more segments of memory used to store data.

1. What is the variable?

Variable specifies the name of a storage location that stores a specific type of value. In Go, there are multiple syntaxes for declaring variables.

 

2. Declare a single variable

(2.1) syntax for var name type to declare a single variable

package main

import "fmt"

func main() {
  // Variable declaration var age int fmt.Println("my age is", age) }

Running programs online

my age is 0

Statement var age int declares a variable of type int, named age. We haven't assigned a value to this variable yet. If a variable is not assigned, Go automatically initializes it, assigning a Zero Value of that variable type. In this case, age is assigned to 0. If you run the program.

 

Variable can be assigned to any value of this type. age in the previous program can be assigned to any Integer Value.

package main

import "fmt"

func main() {
    // Variable declaration
     var age int
    fmt.Println("my age is", age)
    // assignment
       age = 29
    fmt.Println("my age is", age)
    // assignment
       age = 35
    fmt.Println("my age is", age)
}

Running programs online

my age is 0
my age is 29
my age is 35

 

(2.2) declare variables and initialize

Variables can be declared with initial values
The syntax of var name type = initialvalue is used to declare variables and initialize them.

package main

import "fmt"

func main() {
    // Declare variables and initialize
    var age int = 29

  fmt.Println("my age is", age)
}

Running programs online

my age is 29

In the above program, age is an int type variable with an initial value of 29. If you run the above program, you can see the following output, verifying that age has been initialized to 29.

 

(2.3) Type Inference

If a variable has an initial value, Go can automatically infer the type of variable that has an initial value. Therefore, if a variable has an initial value, type can be omitted from the variable declaration.

If the syntax of variable declaration is var name = initialvalue, Go can automatically infer the type of variable based on the initial value.

In the following example, you can see that in line 6, we omit the int type of the variable age, and Go still infers that it is int type.

package main

import "fmt"

func main() {
    var age = 29 // Inferable type

    fmt.Println("my age is", age)
}

 

3. Declare multiple variables

(3.1) Go can declare multiple variables through one statement. The syntax for declaring multiple variables is var name1, name2 type = initialvalue1, initialvalue2.

package main

import "fmt"

func main() {  
    var width, height int
    fmt.Println("width is", width, "height is", height)
    width = 100
    height = 50
    fmt.Println("new width is", width, "new height is ", height)
}

Running programs online

width is 0 height is 0  
new width is 100 new height is  50

 

(3.2) in some cases, we may want to declare different types of variables in a statement. Its syntax is as follows:

var (  
    name1 = initialvalue1,
    name2 = initialvalue2
)

Using the above syntax, the following program declares different types of variables.

package main

import "fmt"

func main() {
    var (
        name   = "naveen"
        age    = 29
        height int
    )
    fmt.Println("my name is", name, ", age is", age, "and height is", height)
}

Running programs online

my name is naveen , age is 29 and height is 0

Here we declare name of string type, age and height of int type

 

4. Short statement (recommended)

(4.1) Go also supports a concise form of declaring variables, called Short Hand Declaration, which uses the: = operator.

The short syntax for declaring variables is name: = initialvalue.

package main

import "fmt"

func main() {  
    name, age := "naveen", 29 // Brief statement

    fmt.Println("my name is", name, "age is", age)
}

Running programs online

my name is naveen age is 29

Short declaration requirement: all variables to the left of the = operator have initial values. The following program will throw the error cannot assign 1 values to 2 variables because age is not assigned

 

(4.2) syntax requirements for short declaration: at least one variable to the left of the = operator is undeclared. Consider the following procedure:

package main

import "fmt"

func main() {
    a, b := 20, 30 // Declare variables a and b
    fmt.Println("a is", a, "b is", b)
    b, c := 40, 50 // b has declared, but c has not
    fmt.Println("b is", b, "c is", c)
    b, c = 80, 90 // Assign new values to declared variables b and c
    fmt.Println("var b is", b, "c is", c)
}

Running programs online

a is 20 b is 30  
b is 40 c is 50  
changed b is 80 c is 90

But if we run the following program:

package main

import "fmt"

func main() {  
    a, b := 20, 30 // Statement a and b
    fmt.Println("a is", a, "b is", b)
    a, b := 40, 50 // Error, no undeclared variables
}

Running programs online

no new variables on left side of: = will be thrown after running above, because variables of a and b have been declared, and there are no undeclared variables on the left side of: =.

Variables can also be assigned at run time. Consider the following procedure:

package main

import (  
    "fmt"
    "math"
)
func main() {  
    a, b := 145.8, 543.8
    c := math.Min(a, b)
    fmt.Println("min mum value is ", c)
}

Running programs online

min mum value is  145.8

 

(4.3) because Go is a Strongly Typed language, variables of one type are not allowed to be assigned values of other types. The following program will throw the error cannot use "naveen" (type string) as type int in assignment, because age was originally declared as int type, but we tried to assign it a value of string type.

package main

func main() {  
    age := 29      // age is of type int
    age = "wide" // Error, trying to assign a string to a variable of type int
}

 

summary

1. The standard format of variable declaration in Go language is:

var variable name variable type

The variable declaration starts with the keyword var, sets the variable type, and does not need semicolon at the end of the line.

2. Batch format:

Think it's trivial to declare variables with var in every line? It doesn't matter. There's another way to define variables for lazy people:

var (
    a int
    b string
    c []float32
    d func() bool
    e struct {
        x int
    }
)

Using the keyword var and parentheses, you can put a set of variable definitions together.

3. Short format

In addition to the var keyword, you can use a shorter variable definition and initialization syntax

Name: = expression

Note that short variable declaration has the following limitations:

(1) , define variables, and initialize them explicitly.

(2) , cannot provide data type.

(3) , can only be used inside a function.

Tags: Go

Posted on Sun, 05 Apr 2020 16:41:27 -0700 by fatherlyons