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 variablePrimitive type variable
int a
String b
float c
Animal a = new Animal()
Student s = new Student()
private int a
public String b
protected float c
private Animal a
Variable Naming
- Variable names are case-sensitive
- An unlimited-length sequence of Unicode letters and digits
- Beginning with a letter, the dollar sign "$", or the underscore character "_".
- No special characters allowd as identifier of variable
- 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)
}
Since creating a new variable with a starting value is so common Go also supports a shorter statement:
y := "Hello Go!"
Declare multiple variable at same time
var (
name string
age int
location string
)
Function / Local Variable
package main
import "fmt"
func main(){
var x int = 10
fmt.Println(x)
}
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)
}
- Name must be start with letter
- Name may contain letter, number and underscore (_)
- Name is character sensetive Num and num consider as two different variable