DEV Community

loading...
Cover image for Learning Go from JavaScript

Learning Go from JavaScript

cali3192 profile image Cali Tabilas ・3 min read

This is will be a series quick, little blogs to document what it is like learning Go from a JavaScript background. I hope to keep each post <5min. I'll try to release posts on a weekly/biweekly cadence to keep me accountable on this journey.

One of the reasons why I wanted to learn Go is because I work with enterprise TypeScript/JavaScript aka flexible languages with Dynamic types. I wanted to challenge myself to work with a statically typed language so that different checks occur at compile vs run time. Another interesting distinction between JavaScript and Go is that Go has a smaller memory footprint and provides the convenience of statically linked binaries.

Getting Started

To download Golang, visit their homepage here and simply follow the installation instructions for your machine. I personally use VSCode as my primary editor to run Go. I first had to add the golang.go extension to get language support. As of today, this extension has 4.5M downloads.

Download and install

Directory Structure

For the purpose of creating a Hello World project, we will just need to create a root directory and a main file with the .go file extension.

$ mkdir root 
$ cd root
$ touch main.go

root
        main.go
Enter fullscreen mode Exit fullscreen mode

File Structure

The primary requirement for a Go program is that it must have a main function as part of the main package. This main function serves as an entry point for executing our go file.

In its simplest form, there are 3 components to a Go file.

  1. Executable - Your file will either be executable or a non-executable utility.
    • The main package will contain a main function that denotes the start of a program.
    • Any package other than main package is a non-executable package and it is not self-executable. Can also be called anything other than main.
    • Non-executables more or less serve as utility functions.
  2. Imports - Import statements let us bring in external libraries that we can leverage in our program. Some useful libraries for starting out:
    • fmt
    • math
    • time
  3. Main - Entry point and Go looks to execute this main function
// main.go

// 1. executable files will be called main
package main

// 2. imports to be declared before main 
// bringing fmt to print 
import "fmt"

// functions can be defined outside main
// in this case, defining a simple helloWorld
func helloWorld(greeting string) {
    fmt.Println("Hello, ", greeting)
}

// 3. define main function and Go looks to execute this funct
func main() {
    // passing an input to the helloWorld func
    helloWorld("Golang")
}
Enter fullscreen mode Exit fullscreen mode

Running the Program

Running Go is as simple as using the go run  command and pointing to your go file.

$ go run main.go
Enter fullscreen mode Exit fullscreen mode

After running the above command, we will see the following in the terminal

Hello, Golang
Enter fullscreen mode Exit fullscreen mode

💡 An executable program in Go can be created using go build. The file will look like a series of binaries.

Alternatively, we can create a binary executable file, you can use the go build command.

$ go build main.go
Enter fullscreen mode Exit fullscreen mode

This executable file can be found in the current directory. You will likely get a warning if you try to open this new main file because it is binary. Our new directory will now look like:

root
    main.go
    main // this is the executable file
Enter fullscreen mode Exit fullscreen mode

Next Blog:

I will be exploring the basic Go data structures, the differences in variable swapping with Go vs JavaScript, and some basic Go operators.

Helpful Resources

Go Tour

The Go Tour is extremely useful in learning the basics of Go from the site itself.

Effective Go

Similar to Effective TypeScript, the Effective Go highlights best practices and how to leverage the best parts of Go.

Discussion (1)

pic
Editor guide
Collapse
chseki profile image
Christian Seki

I came from JS/TS world too and nowdays I've been playing with Go a lot.
I really enjoy reading about comparing golang implementation to array iterations built in methods from Javascript since in Go we just have the well known "for" looping construct, maybe you can write something about it, nice article btw !