Skip to content

Latest commit

 

History

History
308 lines (202 loc) · 5.87 KB

File metadata and controls

308 lines (202 loc) · 5.87 KB

Getting Started With Go

Let's study Go

A huge thank you to Calvin!



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

    sudo add-apt-repository ppa:longsleep/golang-backports
    sudo apt update
    sudo apt install golang-go

MacOS


2. Check Go version

Ubuntu

chloe@chloe-XPS-15-9570 ~
$ go version
go version go1.14.2 linux/amd64

MacOS

~ via ⬢ v14.15.1
➜ go version
go version go1.15.6 darwin/amd64

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

export GOPATH=$HOME/go

PATH setup

export PATH=$PATH:$GOPATH/bin

4-2. MacOS $GOPATH setup

Add the following to ~/.zshrc

export GOPATH="/Users/chloe/workspace/go"
export PATH="$GOPATH:$PATH"

+

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

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

2. Packages and imports

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

    • ex)

      something.go

      package something
      
      import "fmt"
      
      func saySeeya() {
       fmt.Println("See ya!")
      }
      
      func SayHello() {
       fmt.Println("Hello!")
      }

      main.go

      package main
      
      import (
       "fmt"
      
       "github.com/chloe-codes1/go101/something"
      )
      
      func main() {
       fmt.Println("Hello World")
       something.SayHello()
      }


Console output functions - println, print

Let's output

ex)

package main
import "fmt"

func main() {
 fmt.Print("Hello goorm!")
}
  • 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)

    package main
    
    func main() {
      var num1 int = 1000
      var num2 int = 1413
    
      println("Let's do addition ~ ", num1 + num2)
    }
    chloe@chloe-XPS-15-9570 ~/Workspace/Go/Go101
    $ ./calc
    Let's do addition ~  2413

fmt package

  • Package for input/output
  • How to use
    • Write import "fmt" below package main



+

Useful Go resources recommended by Calvini