Golang or GO is a programming language developed in 2009 by three Google engineers to fulfill its high-performance needs.

Its main goal is performance, which can be achieved by combining simplicity with native parallelism (multi-threading and multi-processing).

The main source of documentation is the official website [Link] that also offers a web code test and syntax check [Link].

For developing using VS Code [Link], consider installing the extension GO, by Lukehoban [Link].

Commands for running, testing for data race, building, installing, and downloading external code:

go run src/program.go
go run -race src/program.go
go build src/program.go
go install src/program.go
go get github.com/nsf/gocode

KEEP IN MIND

  • GO defines variables, functions, etc as GLOBAL when the first letter of the name is capitalized, and exclusive in the context if in lower case.
  • The manipulation of strings is not straightforward and will require importing the modulestrconv [Link]. Otherwise, you will have to deal with the UTF-8 representation number of each character.
  • GO compilation is very fast and the output binary contains everything it needs to run. No additional file is required.
  • It is made for creating applications that run in servers and do not support officially any kind of GUI.
  • Each project directory will be as follows:
    • ProjectName (the root of the project)
      • src (source code)
        • The .go files must be located here.
        • Including the downloaded codes from go get.
      • bin (installed programs)
        • Compiled files will be placed here is used go install.
      • pkg
        • mod
          • Additional packets and modules will be stored here.

DECLARATION

  • const varName int
  • var varName int = 10
  • var varName1, varName2 = 10, 20

VARIABLE TYPES

  • bool
  • string
  • int int8 int16 int32 int64
  • uint uint8 uint16 uint32 uint64 uintptr
  • byte
  • rune
  • float32 float64
  • complex64 complex128

LOGICAL OPERATORS

  • &&
  • ||
  • !

COMPARISON OPERATORS

  • ++
  • !=
  • <
  • <=
  • >
  • >=

ARITHMETIC OPERATORS

  • +
  • *
  • /
  • %

BIT OPERATORS

  • &
  • |
  • ^
  • &^
  • <<
  • >>

ARRAYS, SLICES, and RANGES

var arrayName = [3]int{1, 2, 3}
var sliceNameA = arrayName[1:2]
var sliceNameB = []int
sliceNameB = append(sliceName, 10, 20)
sliceNameC = append(sliceNameA, sliceNameB)

SIMPLIFIED DECLARATION

varName := 1
varName := 1.5
varName := 1 + 2i
strName := "String"
strName := `Multiline
string`

IMPORT

import "fmt"
import (
  "math/rand"
  "embed"
  "log"
  "net/http"
  "io"
)

 

POINTERS

varName1 = 123
varName2 = &varName1 // refer to the memory address
varName3 = *varName2 // derefer value in the pointer address

PRINT

fmt.Println("First line", "Second line")
fmt.Printf("%d hex:%x bin:%b fp:%f sci:%e",11,11,11,11.0,11.0)

IF, ELSE IF, and ELSE

if varName == "A" || varName == "B" {
  // code
} else if varName == "C" {
  // code
} else {
  // code
}

SWITCH

switch varName {
  case "A":
    // code
    fallthrough
  case "B":
    // code
  default:
    // code
}

FOR LOOPS

for i != 50 {
  // code
  i ++
}
for i := 0; i <= 10; i++ {
  // code
}

FUNCTION

func functionName(argName int) int {
  // code
  return
}

ANONYMOUS FUNCTION

Anonnymous Function
func (){
  // code
}

VARIADIC FUNCTION

func functionName(args ...int) (string, int) {
  for _, v := range args {
    // code
  }
  return strName varIntName
}

HELLO WORLD

package main 

import "fmt"

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

CHANNELS

packet main

import "fmt"
import "sync"

var waitGroup = sync.WaitGroup{}

func main {
  channelName := make(chan int)
  waitGroup.Add(2)
  go func(){
    i := <- channelName
    fmt.Println(i)
    waitGroup.Done()
  }()
  go func(){
    channelName <- 99
    waitGroup.Done()
  }()
  waitGroup.Wait()
}

HTTP LISTENER

package main

import "net/http"

func main() {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("Welcome!"))
   })
   err := http.ListenAndServe(":80", nil)
   if err != nil {
      panic(err.Error())
   }
}