Skip to content

Instantly share code, notes, and snippets.

@quangkeu95
Last active December 8, 2017 07:06
Show Gist options
  • Save quangkeu95/cad8625bad094083e78d67511cf974f5 to your computer and use it in GitHub Desktop.
Save quangkeu95/cad8625bad094083e78d67511cf974f5 to your computer and use it in GitHub Desktop.

INTRODUCTION

Workspaces

Sẽ có 2 kiểu program trong Go:

  • Executables: là chương trình có thể chạy trực tiếp từ terminal.
  • Libraries: đóng gói thành package để các chương trình khác có thể gọi tới. Basically, Go workspace contains three directories:
  • src: Contains Go source files.
  • pkg: Contains package objects
  • bin: Contains Go executable commands.

We will keep source codes in directory named $GOPATH/src/github.com/user/project1 so we can call our package in another one by using: import ("github.com/user/project1")

In order to build and install a package, you run: go install $GOPATH/src/github.com/user/project1 or more simply go install github.com/user/project1

The command builds the package and produces a executable command in our workspace's bin directory.

About package

Go package name is the last element in import path, an executable command must always use package main, for example the "math/rand" package will contains a file start with package rand.

Exported Names

In Go, a name is exported if it begins with capital letter, for example Pi in math package.

When importing packages, you can refer only to package's exported name, any unexported name can only be accessible inside the package.

Go Declaration Syntax

Variables

var <variable_name> <type> Examples:

var x1, x2 int = 1, 2
var y *int
var z [3]int

Function

func <function_name>(args1 type1, args2 type2) <function_type> Examples:

func main(x int, str []string) int
func main(int, []string) int

Notes:

  • Inside a function := can be used to replace var keyword.

Pointer

  • Pointer holds memory address of a value.
  • Pointer have zero value is nil Examples:
    var p *int
    i := 42
    p = &i
    fmt.Println(*p) // read i through the pointer p
    *p = 21         // set i through the pointer p
    

Struct

Example:

type Vertex struct {
    X int
    Y int
}

func main() {
    v := Vertex{1, 2}
    v.X = 4
    fmt.Println(v.X)
}

Slice

  • Declare a slice: letters := []string{"a", "b", "c", "d"}

  • Declare a slice using make func (func make([]T, len, cap) []T):

s = make([]byte, 5, 5)
// s == []byte{0, 0, 0, 0, 0}

Type Conversion

var x int = 10
var y float64 = float64(x)
var z uint = uint(y)

Constant

Examples:

const Pi = 3.14

Constant cannot be declared using the := syntax

Loop

Examples:

//For loop
sum := 0
for i := 0; i < 1000; i ++ {
 sum += i
}

//While loop
sum := 0
for sum < 1000 {
 sum += sum
}

//Infinity loop
for {
 //Do sth
}

//Switch
t := time.Now()
   switch {
   case t.Hour() < 12:
   	fmt.Println("Good morning!")
   case t.Hour() < 17:
   	fmt.Println("Good afternoon.")
   default:
   	fmt.Println("Good evening.")
   }

Defer

  • defer ensure a function be performed after its surrounding function returns, usually defer is used as cleanup actions.
  • Defer stack uses last-in-first-out order, for example:
func b() {
   for i := 0; i < 4; i++ {
       defer fmt.Print(i)
   }
}

This will returns "3210"

Example:

f := createFile("/tmp/defer.txt")
defer closeFile(f)
writeFile(f)

Go futher Go programming language specifications Effective Go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment