Golang for Javascript Developers

ยท

3 min read

Golang for Javascript Developers

It is always better to start with a definition and later elaborate on those. Thus we will first try to define JavaScript and Golang both and later we will go through the examples to see the difference.

Now, as google says, JavaScript is a dynamically typed interpreted language, whereas Golang is a statically typed compiled language.

What does it mean? It means, to run the golang code, you have to compile it. But before all of that, you have to write the golang code first ๐Ÿ˜œ. So, today we will learn how to write golang code as a JavaScript developer and how it is different from JavaScript

1. Variables

You might already know the javascript way of declaring and initializing variables

variables.js.png

Now, we will see the golang way of declaring those variables

variables.go.png

As you can see we have to mention the type of the variable while we are declaring it.

But, Golang also has a concept called type inference, where if a variable has an initial value, Golang will automatically be able to infer the type of that variable using the initial value

inference.go.png

Golang also has a shorthand to declare variables where you only have to mention the initial value

shorthands.go.png

mention: golang does not use a semi-colon ( ; ) at the end of a line

2. Types

Golang has some predefined basic types and those types are as follows

types.go.png

3. Functions

JavaScript functions are written as follows,

functions.js.png

whereas, in golang you need to write like this

functions.go.png

As you can see golang expects parameter types and return types both for declaring a function. You can also return multiple values in golang

returns.go.png

few more examples of golang functions

func_examples.go.png

4. Packages

Golang has a concept called package. You can import different packages in your code and reuse them.

A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package.

It is somewhat similar to Javascript require or import but not exactly the same

inport.js.png

where

inport.go.png

5. Class/Struct

In Javascript we represent entities using class whereas in Golang we generally do it with the help of struct

class.js.png

The golang counterpart will look something like this

class.go.png

As you can see in Golang we declare a struct and attach functions to it to get the same effect as Java/Javascript class.

5. Printing to stdout (Something simple and fun)

In JavaScript we print to stdout like this

print.js.png

In Golang you have to import a package named fmt to print something to stdout

print.go.png

These are the basic differences in JS and Golang which will let you get started with writing Golang code. But there are lot of features in Golang which I am not able to cover in this blog. If you want to learn those and get good at Golang, I would recommend you to go through this website.

Also if you want to know more about any of the discussed topics, I would like you to go through these websites

  1. Golangbot
  2. Go by example