Learning Go from JavaScript
Tags: tutorial, beginners, go
Published: 2021-03-22 14:47:24

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](https://golang.org/doc/install) 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](https://golang.org/doc/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.

```powershell
$ mkdir root 
$ cd root
$ touch main.go

root
		main.go
```

### **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

```go
// 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")
}
```

### **Running the Program**

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

```bash
$ go run main.go
```

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

```bash
Hello, Golang
```

> 💡 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.  

```bash
$ go build main.go
```

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:

```powershell
root
	main.go
	main // this is the executable file
```

### 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](https://tour.golang.org/basics/)** is extremely useful in learning the basics of Go from the site itself.

### **Effective Go**

Similar to Effective TypeScript, the **[Effective Go](https://golang.org/doc/effective_go)** highlights best practices and how to leverage the best parts of Go.