Go Language Overview

Jul 17, 2024

Go Language Lecture Notes

Introduction to Go

  • Statically typed language: Variable types need to be explicitly declared or inferred; immutable without type conversion.
  • Strongly typed: Operations depend on types; mixing types like integer and string is not allowed.
  • Advantages: Thorough error checking, better code completion, faster execution.

Compilation

  • Compiler: Translates code into machine code, producing a binary file, faster than interpreted languages like Python.
  • Example: Loop counting to 100 million runs significantly faster in Go compared to Python.
  • Quick compilation: Enables a faster test-compile-debug cycle.

Concurrency

  • Built-in concurrency: Using go routines for parallelism without special packages.
  • Example: Running loop concurrently.

Simplicity

  • Philosophy: Concise syntax, fewer lines of code, automatic garbage collection.

Setting Up Environment

  • Installation: Go to the Go website and follow installation instructions for your OS (Linux, Mac, Windows).
  • Verification: Run go version in the terminal to ensure Go is installed.

Project Initialization

  • Creating a project: Initialize with go mod init <module_name>, often named after GitHub repo location.
  • Go mod file: Lists the module name and Go version.
  • Editor options: Vim, Neovim, VSCode, etc.
  • Main package: Entry point defined by main function in the main package.
  • Imports: Import packages using syntax like `import