Menu Close

Go – Variables in go golang

In this tutorial, We are going to learn about variables and it’s uses in go golang. A variable is a storage location in memory with name and specific type

Go variables are explicitly declared. 

Normally, We declare variables in go using var keyword then specify the variable name and at last data type. Variable name must start with letter or underscore (_) and after that it may contain letters, numbers.

If you don’t assign value to variable at the time of declarationgo assigns default value to the variables of their respective type.

We can declare variables in multiple ways in go golang.

1:) Declaring a single variable.

Syntax:

var variable_name type

Example with code:

package main

import "fmt"

func main() {

  // variable declaration as int type
  var roll_number int

  roll_number = 10

  fmt.Println(roll_number)
}

Output:

10

2:) Declaring multiple variables at once.

Syntax:

var variable_name_1, variable_name_2, variable_name_3 type

Example with code:

package main

import "fmt"

func main() {

  // multiple variable declaration as int type
  var roll_number, marks int

  roll_number = 10

  marks = 100

  fmt.Println(roll_number)

  fmt.Println(marks)
}

Output:

10
100

3:) Declaring and initialising multiple variables at once. 

Syntax:

var variable_name_1, variable_name_2, variable_name_3 = value_1, value_2, value_3 

Here, It also infers types of Initialised Variables in go. It is also called dynamic type declaration.

Example with code:

package main

import "fmt"

func main() {

  // multiple variable declaration as int type
  var roll_number, marks = 10, 100

  var avg float64 = 20.0

  fmt.Println(roll_number, marks, avg)
 
}

Output:

10 100 20

4:) Shorthand for Declaring and Initializing a Variable in Go

Shorthand declaration has following given below features at single line:

  • Declaring a variable.
  • Initializing this variable.
  •  Inferring this type.

Syntax:

variable_name := value

Example with code:

package main

import "fmt"

func main() {

  avg := 20.30

  number, marks := 10, 40

  fmt.Println(avg, number, marks)

}

Output:

20.3 10 40

5:) Re-assigning value into shorthand variables.

Example with code:

package main

import "fmt"

func main() {

  number, marks := 10, 40

  fmt.Println(number, marks)

  // Re-assigning value to variables
  number, marks = 30, 50

  fmt.Println(number, marks)

}

Output:

10 40
30 50

6:) Type casting of go variables

We can do type casting explicitly in go.

Example:

package main

import "fmt"

func main() {

  var average_marks float64 = 70

  fmt.Println("Average marks before: ", average_marks)

  var number int = 40

  average_marks = float64(number)

  fmt.Println("Average marks after:  ", average_marks)

}

Output:

Average marks before:  70 
Average marks after: 40

7:) type aliasing in go

We can declare alias to any built-in function in go.

Syntax:

type alias_name alias_to

Example:

type int int64

In the above example, go will create int type which will duplicate of the type int64. We can use this type as a normal Go type and declare variables

Example with code:

package main

import "fmt"

type int int64

func main() {

    var i int = 100

    fmt.Printf("i has value %v and type %T", i, i)
}

Output:

i has value 100 and type main.int

In the above example, we have defined the type int as an alias to int64 type and create a variable i of type int

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println

To learn more about golang, Please follow given below link:

https://www.techieindoor.com/go-lang-tutorial/
Posted in golang

Leave a Reply

Your email address will not be published.

Contact Us