DEV Community

Ruby Valappil
Ruby Valappil

Posted on • Originally published at Medium on

How to Get Started with GoLang as a Java Developer

Few simple steps to get started with GO.


Photo by Chinmay Bhattar on Unsplash

For quite some time, I have wanted to learn GO. The programs that I used to read, appeared to be simple enough to understand.

As Java developers, we are quite used to lengthy syntaxes. We don’t forget the “;” and our file name and public classes have the same name. We also take care of the visibility and access modifiers even for a simple Hello World program.

I just started learning GO and find it much relatable to Java in terms of the package and import declaration and the method /function syntax. With no classes to define and semicolons to add, I find GO much quicker to develop, at least for a simple application like “Hello World”.

Let’s try to implement a simple Hello World program in GO using a text editor and a Code Editor.

Step 1: First things first, let’s download and install Go on our local machine. Installation steps for all the different OS’s are given in the official guide at https://go.dev/doc/install.

I have installed the below-given version of golang, (use go version command to check that)

go version go1.17.7 darwin/amd64
Enter fullscreen mode Exit fullscreen mode

By default, go would be installed at the below path,

usr/local/go
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the project structure

A GO project must have 3 directories — bin, pkg, and src.

Under the src folder, we will create our application folders and files.

bin folder stores all the binaries generated from the application and the downloaded dependencies.

pkg is an intermediate folder that holds the compiled objects from src directory.

A simple project structure would look like the one below,


the directory structure of hello-world repository

Step 3: Create project folder under src

All the code that’s part of our application will go under the src folder. Let’s create a folder under the src directory which will follow the naming conventions similar to the one we use in java applications — com/something/something.

In this article we are just creating a single package under the src package and named it main, it could be any other name as well. Create a file with *.go extension.

The first thing in a go file must be the package declaration, followed by the imports under two simple brackets “(“ and “)”. Next, write the “main()” function that would use the fmt package to call “Println” function.

“main()” function must be declared in the main package else the compiler would complain.

Here’s the complete code to this hello world program

package main

import ("fmt")

func main(){
 fmt.Println("Hello World")
}
Enter fullscreen mode Exit fullscreen mode

To run this program, go to the program’s directory and execute the following command.

go run Main.go
Enter fullscreen mode Exit fullscreen mode

Output to the above command would be — Hello World

Step 4 (Optional): Use a Code Editor

I’m quite used to using STS and Eclipse for Java programming so at first, I tried to install the go plugin to STS and create a Hello World program. My experience with Go on STS/ Eclipse was really bad, now it could be because I don't know how to configure a Go project correctly on that IDE but overall the experience was not good and the error messages didn’t make it easy to figure out the root cause.

So I did some research on the best tools to use with Go. Turns out there are more code editors than IDE as such and the best IDE is GoLand by JetBrains, the same folks who developed IntelliJ.

A few of the popular code editors are vim, govim, and VS Code.

I have never tried VS Code before and always wanted to try it so I decided to try VS Code for GO programming.

Firstly, I installed the go plugin for VS Code


Go Extension for VS Code

Then wrote the same program shared in Step 3, using the Terminal tab in VS Code, executed the GO program.

Conclusion

I like the fact that GO is a simple language, especially coming from a Java background.

A hello world is not even the tip of the iceberg but it’s a start. While learning human languages we tend to compare the new language to the one we already know, it makes learning easier through comparison. I’m trying to follow the same approach to see how to program the specific actions that I usually carry out using Java in GO.

Check this repository for my GO experiments.


Top comments (0)