Over a million developers have joined DZone.

Go Language for Java Developers Part 5

Part 5 of a 5 part series that explains the Go language to Java developers.

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

A variable is a storage location that holds a value. The set of permissible values is determined by the variable's type.

Java language has primitive types and objects, and 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 variables in 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
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 a variable, x is the name of variable and int is the type of variable.
In Line 2, we are assigning the value in x variable.
In Line 3, we have declared a string variable and assigned 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 the above shorter syntax Go compiler will automatically identify that y is a variable of type string and its 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 places 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 the y variable, while x is local variable only accessible inside the 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 Names

  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

More Reading

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
golang ,java

Published at DZone with permission of Ketan Parmar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}