Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Go Language for Java Developers Part 2

DZone's Guide to

Go Language for Java Developers Part 2

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

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Normally the first program you write in any programming language is "Hello World". Hello World is simple program which prints "Hello Word" text to the console / screen.

  Java: Hello World 
 As Java Developer you can easily understand following code. There is no need to explain, right?

package com.kpbird.gotutorial; 
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");        
    }
}


To compile & execute above code, you need to write following two commands in the Terminal.
javac Main.java
java Main

Go: Hello World 
Create the directory where you will save the code. Create file named "main.go" with the following content:
package main  // line 1
import "fmt" // line 2
func main() { // line 3
    fmt.Println("Hello World") // line 4
} // line 5

To execute above code, you need to write following command in the Terminal
go run main.go


You should see Hello World in terminal. If you don't see Hello World displayed then you may have mistake in typing program. The Go compiler will give you hint about where the mistake lies.

Understand Go : Hello Word program 

1. Package Name (Line 1): The Go program starts with a package declaration. Every Go program must start with a package declaration. The package declaration is used to organise source code. In Java we follow relative path but with "." (dot) format like com.kpbird.gotutorial represent physical path com/kpbird/gotutorial. While in the Go language the package name represents only the current folder name "gotutorial". The Go language supports two type of programs
  1. Go Libraries
  2. Go Executables
The package main is a way to tell compiler that this Go program is executable. When you declare "package main" you don't need to create folder / directory called "main".

2. Import (Line 2): The import keyword is used to include code from another package or library. The "fmt" package is the short form of Format. It is used to format input and output. In Java, we don't need to write java.lang package explicitly compiler will do it behalf of us.

3. fund main() (Line 3): Functions are the building blocks of the Go language. All functions start with keywords "func". func followed by name of function. In our case "main" is the name of function. If the function takes any argument we need to write in parentheses. Main is a special function because main gets called when we execute Go program. It's like public static void main() in Java.

4. fmt.Println (Line 4): Line 4 has three component
  1. fmt package name.
  2. Println function name
  3. String argument "Hello World".
In Java, we use System.out.println().

Java vs Go 
We should not compare both languages because both are different. Go Language is a functional language in which functions are first-class citizen. Java Language is Object Oriented language in which Class / Object are first-class citizen.


To execute Java program, first we need to compile using "javac" command, after that we need to use "java" command execute a program.


To execute Go program, we just need to write Go run main.go. Single command will compile and execute program. To build program Go language has command build. It will generate native executable file as per the platform, that you can directly execute.
$Go build main.go 
$./main

The Go Language don't have semi-colon (;) to end the statement. This means that you can't write two statement in single line.

In Java Language, As we have semi-colon (;) to end the statement we can write entire code in single line.

In the next article we will learn about data types in Go Language.

More Reading

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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 DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}