go lang

How to create variables in go?

In this tutorial shows how to create and use variables in go language.

Variables in GO lang:

variable is any entity that can take on different values. Variables are used to hold the values. Variables are like labels given to that values. Go uses the var keyword to declare a list of variables. We can also use the := shorthand syntax to declare variables. Go can infer the data type from the right side of the assignment.

Declare variables in Go:
package main
import "fmt"

func main() {

    var i int = 10
    var w float64 = 10.5

	fmt.Println(i)
	fmt.Println(w)
}

In this example we declared two variables one in of int type and another is float64 type using var keyword.

Output:

$ go run variablesbasic.go
10
10.5
Declare multiple variables in GO:

In the above example, we declared one single variable. In this example, we will declare multiple variables.

package main

import "fmt"

func main() {

    var x, y, z = 24, 25, 26

    var (
        name       = "Mahesh Ligade"
        occupation = "Engineer"
    )

    fmt.Println(x, y, z)
    fmt.Printf("%s is a %s\n", name, occupation)
}

Output:

$ go run multiplevar.go 
24 25 26
Mahesh Ligade is a Engineer
shorthand variables in GO:

Inside a function/method, the := short assignment statement can be used in place of a var declaration with implicit type.

package main

import "fmt"

func main() {

    name := "Mahesh Ligade"
    age := 29

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

Output:

$ go run shorthandassignment.go 
my name is  Mahesh Ligade
Age is  29
Type inference in GO:

As I said above Go can infer data based on values. In this example, we are demonstrating inference with multiple data types. In this example, we have imported two packages one in fmt and the other one is reflect, which is useful to find the type of the variable.

Here we declared two var without dataytpe and we are printing datatype of these var using reflect package.

package main

import (
    "fmt"
    "reflect"
)

func main() {

    var name = "Mahesh Ligade"
    var age = 29

    fmt.Println("Type of name ",reflect.TypeOf(name))
    fmt.Println("Type of age ",reflect.TypeOf(age))

    fmt.Printf("%s is %d years old\n", name, age)
}

Output:-

$ go run typeinference.go 
Type of name  string
Type of age  int
Mahesh Ligade is 29 years old
Default value of Go variables:

In the above examples if you notice we explicitly assigned some value to variables. In this example, we are not assigning any values and we are trying to display the default value of those vars. We declared four basic datatype vars such as int, boolean, String, and float.

package main

import "fmt"

func main() {

    var age int
    var isValid bool
    var name string
    var money float64

    fmt.Println(age, isValid, name, money)
}

Output:-

$ go run defaultvalue.go 
0 false  0

From the above output we can said default values based on datatypes are:

  1. 0 – Int types
  2. false – Boolean type
  3. “” – String type
Variables Scope in GO:

Till now we learned basic variable creation with implicit and explicit assignment along with type inference, Now Let us understand different scopes go variables support.

package main

import "fmt"

var nasaword string = "PSLV"

func main() {

    n := 10

    fmt.Println(nasaword)
    fmt.Println(n)

    newScope()
}

func newScope() {

    fmt.Println(nasaword)
}

Output:-

$ go run variablescope.go 
PSLV
10
PSLV

We have two variables one which is declared outside of the main() function whereas the numeric variable has a limited scope of the main() function. The nasaword variable we declared at global scope.

Constants in GO language:

Up to now, you learned variables to hold the values that can be changed. Now you can explore in this section about constant in go language. Constants values can not be changed, unlike a variable. As per the go language convention constants name should be in uppercase.

package main

import "fmt"

func main() {

    var age int = 34
    const WEIGHT = 72

    age = 35
    age = 36

	// uncomment below line and check the second output
    // WEIGHT = 75

    fmt.Println(age, WEIGHT)
}

Output:

$ go run constantsgo.go 
36 72

$ go run constantsgo.go 
# command-line-arguments
./constantsgo.go:13:12: cannot assign to WEIGHT
Conclusion:

In this tutorial we learned how to declare and use variables and constants in go.

Source Code:
https://github.com/maheshwarLigade/spring-boot-examples
$ git clone https://github.com/maheshwarLigade/GoLanguage-tutorials
$ cd GoLanguage-tutorials
$ go run defaultvalue.go