DEV Community

Cover image for Golang for JavaScript developers - Part 1

Golang for JavaScript developers - Part 1

Deepu K Sasidharan on December 04, 2019

Originally published at deepu.tech. If you are a JavaScript developer thinking about learning another programming language, then Golang is a great...
Collapse
 
marcellothearcane profile image
marcellothearcane
if val := getVal(); val < 10 {
    return val
} else {
    return val + 1
}

This is not possible in JS

What does it do?

Collapse
 
vinceramces profile image
Vince Ramces Oliveros

the variable is scoped to the if statement. Thus, it is much more readable. might be that the getVal() function would be like this.

func getVal() int{
    // return any number
    return 0
}

compare to javascript

// given by the example. the `val` is mutable
let val = getVal()
if(val < 10){
   return val
}
else{
  return val + 1
}
Collapse
 
nmhillusion profile image
nmhillusion

in golang,
after if block, variable val continues existing? Or it just exists in the if block?

Thread Thread
 
vinceramces profile image
Vince Ramces Oliveros

val is no longer accessible after the if statement. the variable is scoped to the if block

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Thanks

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Sorry, I should have explained it better. I'll update the post

Collapse
 
bradtaniguchi profile image
Brad

Awesome article, the only thing I feel like is missing the reason why a JavaScript developer should learn GoLang over other languages. 🤔

Collapse
 
freedom profile image
Freedom

A good reason for me, to port a backend Nodejs to a single Go binary, I can run without any dependencies or installation e.g. virtual machine/runtime, this help to reduce security vulnerabilities and improve reliability.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Well, IMO there is no strong reasons to specifically learn Go except that it is easier to start with. But IMO JS is not a perfect language and hence JS developers would greatly benefit by learning a few other languages which will help to make them more pragmatic. It helped me for sure.

Collapse
 
erebos-manannan profile image
Erebos Manannán

Golang has a massive number of benefits over many other languages. However, "a JavaScript developer" is too vague a concept to list any specific benefits to them.

Some significant benefits:

  • Quick compilation time
  • Good ecosystem with lots of well made libraries
  • Widespread standardization and tooling for e.g. automatic code formatting and static analysis
  • Compiles easy to distribute binaries without external dependencies
  • Cross-compilation is easy in most cases (I believe as long as you don't link to C code), so you can just set up one build pipeline and loop through a number of GOOS and GOARCH variables and get the build results easily
  • Concurrency is incredibly well handled in Golang - no need to worry about threads and other such things manually, but it's easy to control when things are executed in parallel and to communicate between "goroutines"
Collapse
 
bradtaniguchi profile image
Brad

I've learned a bit of Go and I agree it is a clear straight forward language, which makes it easy to learn, very similar to Python in those regards.

Collapse
 
hchev001 profile image
Hamilton Chevez

When and what will part 2 be about, released?

Collapse
 
deepu105 profile image
Deepu K Sasidharan

I mentioned what is covered in part 2 at the end of this post. I'll still need to finish up a few topics there but you can expect it start of next week

Collapse
 
deepu105 profile image
Deepu K Sasidharan
Collapse
 
k_penguin_sato profile image
K-Sato

go Go!!

Collapse
 
deepu105 profile image
Deepu K Sasidharan
Collapse
 
qinlinsen profile image
秦林森

NICE

Collapse
 
dscamargo profile image
Douglas Simon Camargo

I'm JS developer and learning Golang.
Your articles (part1 and part2) help me a lot to understand how to golang works :) Thank you.