DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Go Language for Java Developers Part 2

Go Language for Java Developers Part 2

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

Ketan Parmar user avatar by
Ketan Parmar
·
Apr. 22, 16 · Tutorial
Like (2)
Save
Tweet
Share
11.03K Views

Join the DZone community and get the full member experience.

Join For Free
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

  • Part 1

  • Part 2

  • Part 3

  • Part 4

  • Part 5

Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using AI and Machine Learning To Create Software
  • Asynchronous HTTP Requests With RxJava
  • DevOps Roadmap for 2022
  • Core Machine Learning Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: