Go Variable Declaration

Introduction

Go is a statically typed programming language.

Go variables always have a specific type and we cannot change the type.

The keyword var is used to declare variables of a particular data type.

Here is the syntax for declaring variables:

var name type = expression

We can declare multiple variables of the same type in a single statement.

var fname,lname string

You can use parallel assignment:

a, b := 20, 16 

Using an initializer expression for declaring variables can omit the type.

The following code uses short variable declaration:

letter, state := "A", "B"

The code above uses the operator := for declaring and initializing variables.

The type is determined by the initializer expression.

package main /*  w  w  w. jav a  2  s  .  c  o m*/

import "fmt"

func main() {
  var i int
  var s string

  i = 10
  s = "Canada"

  fmt.Println(i)
  fmt.Println(s)
}

The code above can be rewritten as follows.

package main /*from  ww w  .java2 s . c  o  m*/

import "fmt"

func main() {
  var i int = 10
  var s string = "Canada"

  fmt.Println(i)
  fmt.Println(s)
}

You can omit the variable type from the declaration.

package main//from  ww  w.  java  2  s .  c o m
import (
  "fmt"
  "reflect"
)

func main() {
  var i = 10
  var s = "CSS"

  fmt.Println(reflect.TypeOf(i))
  fmt.Println(reflect.TypeOf(s))
}

The := short variable assignment operator marks a short variable declaration.

In short variable declaration we do not need to use the var keyword or declare the variable type.

package main/*from   www  .  jav  a2 s .co m*/
import (
  "fmt"
  "reflect"
)

func main() {
  name := "CSS"
  fmt.Println(reflect.TypeOf(name))
}

Go allows you to assign values to multiple variables in one line.

package main //from w  w w.  j a  v a2  s  . c  om

import (
  "fmt"
)

func main() {
  var fname, lname string = "f", "l"
  m, n, o := 1, 2, 3
  item, price := "CSS", 42

  fmt.Println(fname + lname)
  fmt.Println(m + n + o)
  fmt.Println(item, "-", price)
}

Variables declaration can be grouped together into blocks.

package main//from ww  w . j a  v a  2  s  . com

import "fmt"

var (
  q  = "CSS"
  quantity = 42
  price    = 42.42
  inStock  = true
)

func main() {
  fmt.Println(quantity)
  fmt.Println(price)
  fmt.Println(q)
  fmt.Println(inStock)
}

More example.

package main //from w  w  w .  j  a  v a 2s  .c o m

import "fmt"

func main(){
    var x int = 1 // Integer Data Type
    var y int    //  Integer Data Type 
    fmt.Println(x)
    fmt.Println(y)
    
    var a,b,c = 5.42, 25.42, 14.42 // Multiple float32 variable declaration
    fmt.Println(a,b,c)
   
    d:="A" // String variable declaration
    e:="B" // Variable names are case sensitive
    fmt.Println(d) 
    fmt.Println(e)
    
    // Multiple type of variable declaration in same line
    food,drink,price:="Pizza","Water",125  
    fmt.Println(food,drink,price)
}

A variable declared within brace brackets {} has a new scope that ends with a closing brace }.




PreviousNext

Related