Wednesday, 1 May 2019

Go Language : := : Short Assignment statement


var i int = 1

By using short assignment statement ':=', you can define above variable 'i' like below.

i := 1

App.go
package main

import (
 "fmt"
)

func main() {
 var i int = 1
 j := 2

 fmt.Println("i : ", i, ", j : ", j)

}

Output
i :  1 , j :  2

Note
a.   Short assignment statement is available within a function, you can’t use it outside a function.




Previous                                                 Next                                                 Home

Go language: Initialize multiple variables in one line


If variables are of same data type, you can initialize them in one line like below.

var i, j, k int = 1, 2, 3
  
App.go
package main

import (
 "fmt"
)

func main() {
 var i, j, k int = 1, 2, 3

 fmt.Println("i : ", i, ", j : ", j, ", k : ", k)

}

Output
i :  1 , j :  2 , k :  3

Previous                                                 Next                                                 Home

Go language: Print value and type of a variable


%v is used to print the value of a variable
%T is used to print the type of a variable.

App.go
package main

import "fmt"

func main() {
 var x int = 10

 fmt.Printf("i : %v, type : %T\n", x, x)
}

Output
i : 10, type : int



Previous                                                 Next                                                 Home