Skip to content

Notes
Coddy - Go Lang Challenges


Go

Go compiles directly into machine code rather than relying on an interpreter like Python. It uses GoRoutines to handle numerous tasks at once.

Installing Go Lang

1) Install Go.
2) Type go version in terminal to verify installation.
3) Open VS Code and press Control + Shift + X.
4) Find the Go Extension.
5) Create a new folder and direct VSCode into it.
6) go mod init projectName.

Creates the go.mod file which stores the name of the module, dependencies, and the go version. Its the Go equivalent of package.json in NodeJS.
7) Create a new file inside the folder called main.go.

Variable Declaration

Standard

The standard declaration in Go uses the keyword var and requires types to be specified however they can be initialised without a value unlike shorthand.

var username string = "User 1" // string explicit typing

Shorthand

Shorthand omits the var keyword and automatically infers variable type based on the value assigned to it.

username := "User 1" // infers from variable type string

My First Program

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}

Hello World! Go Program

Different Bracket Style

While I typically like to use a style as demonstrated below in JS, I discovered in that semicolons are inserted automatically.

JavaScript

if a < b
{
   console.log("Hello World!");
}

Go Example Like Above

Because therefore

if a < b {
   fmt.Println("Hello World!")
}

Go Project - Basic Messaging Application