Go Basics

Let's study Go

A huge thank you to Calvinarrow-up-right!

What is Go Language?

  • Programming language developed by Google

  • Syntax is similar to C language, but designed to handle Concurrency programming easily

    • Concurrency programming

      • Means that the program can do other things first while performing relatively time-consuming operations such as DB requests or network communication like web services

  • Although it's a relatively recently emerged programming language, it's not complex and a practical language

  • Representative projects that utilize Go include Docker and Kubernetes

Why Go?

  • Go aims for a simple, concise, and intuitive language

    • Supports OOP

      • No concepts of class, objects, inheritance

    • Keywords used in Go are only 25, half the level of Java

      • Simple!

    • Although it's a compiled language, the compiler speed is very fast, so it can be used like an interpreter language

  • Has a good build system

    • Build speed is very fast

      • Projects consisting of thousands of lines of code compile in just a few seconds!

    • Code import is based on off url import

      • So it's good for managing dependencies!

        • Go module allows you to hash URLs when importing

          • Similar to importing .lock files in other build systems!

Install Go on Ubuntu/Mac

1. Install Go 1.14

Ubuntu

  • Install using longsleep/golang-backports PPA

MacOS

2. Check Go version

Ubuntu

MacOS

3. Workspace setup

  • Create a directory to proceed with Go projects

    • ex) /Users/chloe/workspace/go

  • Create 3 directories inside that directory as follows

    • bin

      • Where OS-specific executable binary files are stored after source code is compiled

    • pkg

      • Where library files are stored after packages needed for the project are compiled

    • src

      • Where written source code and open source codes are stored

4. Environment variable setup

  • Go has 2 environment variables - GOPATH & GOROOT

  • GOROOT

    • Where Go binaries are located

    • If you install with sudo apt install golang-go, Go is installed in the default path, so no need to modify separately!

      • On Mac, it's installed in /usr/local/go

  • GOPATH

    • User go environment path

    • Need to set where project files are located

    • When importing packages with go import, it's loaded from GOPATH

    • Generally set GOPATH to $HOME/go

      • Important because when doing go install or go import, go looks for GOPATH!

4-1. Ubuntu $GOPATH setup

GOPATH setup

PATH setup

4-2. MacOS $GOPATH setup

Add the following to ~/.zshrc

+

After setting $GOPATH, you can use the go env command to check if it's properly applied!

5. src in $GOPATH

  • Go downloads and stores downloaded codes in $GOPATH/src classified by downloaded domain

    • ex) github.com, golang.org, google-golang.org, etc.

    • For source code I write, I'll create a directory with github user name inside github.com

Getting started with Go

1. Main package

  • Naming the package main.go means you will compile this project and use it

    • That is, all packages except main.go are not compiled!

  • Since main is the entry point, the compiler looks for packages named main first

    • ex) package main

  • Go looks for a function called func main() {}

    • This is the entrypoint of the Go program!

  • Automatically, the compiler first finds and executes the main package and the main function inside it

    • ex)

      main.go

2. Packages and imports

  • To export a function in Go, write it in upper-case

    • ex)

      something.go

      main.go

Console output functions - println, print

Let's output

ex)

  • print / println can be used without importing fmt package

  • println and print differ in the presence of line break

    • To make a line break with print, use \n!

  • Single quotes don't work!

    • Use double quotes

  • print functions can output the results of operations inside functions!

    ex)

fmt package

  • Package for input/output

  • How to use

    • Write import "fmt" below package main

+

Last updated