Go Part 1: Why should We learn Go and some basics!

Advantages of Go

  • Code runs fast

  • Garbage collection

  • Simpler objects

  • Concurrency is efficient

Code runs fast

Software Translation

  • Machine Language: CPU instructions represented in binary. Lowest level language and it's directly executed in the CPU.

  • Assembly Language: CPU instructions with mnemonics.

    • Easier to read -> Concise English like code

    • Equivalent to machine language

    • Basically it is one-to-one mapping to machine language, not completely but very close.

  • High-level language: Commonly used languages (C, C++, Java, Python, Go, etc), in terms of brought term though

They are much easier to use than assembly language or machine language.

They provide us with lots of abstractions that any program would be used to, for instance variables,

Assembly language and machine language do not have variables,

they have memory, and we can put stuff in it,

There's no idea of a type or anything like that in assembly language or machine language,

but high-level languages provide that to us

Now, assembly language, machine language,

they have conditional branches and so on,

but these are generally much harder to use than our standard

if statement that we would see in any normal high-level language or loops or,

for loops and things like this.

we can create these things in assembly and machine language,

but they are harder to write than they would be in a high-level language.

So, high-level languages are basically everything that most people program in.


Now, the language that we're talking about is Go. Go is, of course, a high-level language in this set of three categories, it will be considered high level. Remember that the term high-level is subjective, okay? But we'll call it high level.

Compiled vs Interpreted

Now, a compiled language is a language where the translation from high-level language to machine code happens one time before we execute the code, before we deploy the code, it happens one time.

like in C, C++, Go, Java partially, there's a compiler, and you compile the code. So, somebody writes the source code, they compile it, and then they executes it, and they execute the compiled executable. It happens before we run the code, and then when we run the code, we are just running the machine language instructions directly because they're already compiled into machine language by the compiler.

So, the other way to do this is interpreted, interpretation. In interpreted language what happens is, that instructions are translated while the code is executed. So, what happens is, it happens it adds time to the execution because every time we see an instruction, and say Python, that code, that instruction has to be translated into machine code on a fly, and that takes a certain amount of time just to do that translation. So, in addition to actually executing the instruction, we've got to do this translation from the instruction into the equivalent machine code, so that slows us down.

Efficiency vs. Ease-of-Use

  • Compiled code is fast

  • Interpreters make coding easier

    • Manage memory automatically

    • Infer variable types

  • Go is a good compromise

    • Go is a good compromise between this compiled and interpreted type of language. It's a compiled language, but it has some of the good features of an interpreted language, specifically, it has garbage collection. So, garbage collection is the automatic memory management that we're talking about. So, this memory management, where should memory be allocated, so we'd say, "We need a variable x. Where should we put it in memory? What type of memory we should put it in?"

Manual memory management is hard

  • Deallocate too early, false memory accesses

  • Deallocate too late, wasted memory

Install Go

Download Go

Workspaces

  • Three subdirectories

    • src - Contains source code files

    • pkg - Contains packages (libraries)

    • bin - contains executables

  • Programmer typically has one workspace for many projects

  • It is recommended, not enforced.

  • Workspace directory defined by GOPATH environment variable

    • GOPATH is defined during installation

    • Go tools assume that codes are in GOPATH.

Packages

  • Group of related source files

  • Each package can be imported by other packages

  • The first line of the file names the package

import (
    "package_a"
    "package_b"
)
  • There must be one package called main

  • Building the main package generated an executable program.

  • Main package needs a main() function

  • main() is where code execution starts

package main
import "fmt"
func main(){
    fmt.Printf("Let's go\n") // To print it, similar like print() or console.log()
}

Import

  • import keyword is used to access other packages

  • Go standard library includes many packages

    • fmt
  • Searches directories specified by GOROOT and GOPATH

The Go Tool

  • A tool to manage Go source code

  • Have several commands

    • go build -> compiles the program

      • Arguments can be a list of packages or a list of .go files

      • Creates an executable for the main package, the same name as the first .go file

      • .exe suffix for executable in Windows

    • go doc -> prints documentation for a package

    • go fmt -> format source code files

    • go get -> download packages and install them

    • go list -> lists all installed packages

    • go run -> compiles .go files and run the executable

    • go test -> run tests using files ending with _test.go