Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Go Language For Java Developer Part-5

Tags: variable

Variables

Variable: A variable is a storage location for holding a value. The set of permissible values is determined by the variable's type.

Java Variable

Java language has primitive type and objects, both have different syntax to declare a variable
Primitive type variable
int a
String b
float c
Object type variable
Animal a = new Animal()
Student s = new Student()
Java is object oriented language so that we can have access modifier for variable declaration
private int a
public String b
protected float c
private Animal a
In Java we can declare variable at many places like Local variable, Parameters, Class level, Instance variable.
Variable Naming
  1. Variable names are case-sensitive
  2. An unlimited-length sequence of Unicode letters and digits
  3. Beginning with a letter, the dollar sign "$", or the underscore character "_".
  4. No special characters allowd as identifier of variable
  5. We can't use reserved keywords

Go Variable

Variables in Go are created by first using the 'var' keyword, then specifying the variable name, the type and finally assigning a value to the variable.
package main
import "fmt"
func main() {
var x int // Line 1
x = 10 // Line 2
var y string = "Hello Go!" // Line 3
fmt.Println(x)
fmt.Println(y)
}
In Line 1, var indicate it's variable, x is name of variable and int is type of variable. In Line 2, we are assigning value in x variable. In Line 3, we have declare string variable and assign in single line
Since creating a new variable with a starting value is so common Go also supports a shorter statement:
y := "Hello Go!"
With above shorter syntax Go compiler will automatically identify that y is variable of type string and value is "Hello Go!".
Declare multiple variable at same time
var (
name string
age int
location string
)
Go is not object oriented language so we don't have access modifier like we have in Java. In Go language, We have two place to declare variables 1. Inside Function 2. Out Side Function.
Function / Local Variable
package main
import "fmt"
func main(){
var x int = 10
fmt.Println(x)
}
Global Variable: Any function can access y variable, x is local variable only accessible inside main function
package main
import "fmt"
var y int =10
func main(){
var x int = 10
fmt.Println(x)
fmt.Println(y)
}
func hello(){
fmt.Println(y)
}
Variable Name
  1. Name must be start with letter
  2. Name may contain letter, number and underscore (_)
  3. Name is character sensetive Num and num consider as two different variable



This post first appeared on Ketan Parmar (KP Bird), please read the originial post: here

Share the post

Go Language For Java Developer Part-5

×

Subscribe to Ketan Parmar (kp Bird)

Get updates delivered right to your inbox!

Thank you for your subscription

×