Skip to content

go-tutorials/overview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Overview

Training

Introduction to GO

Overview

IMAGE ALT TEXT

Basic GO

Hello world

  • Every Go program is made up of packages
  • Start running in package main
package main

import "fmt"

func main() {
    fmt.Println("Hello, world")
}

Function

Support to return multiple values

Return 1 value

Return 2 values

package main

import "fmt"

func add(x, y int) int {
  return x + y
}

func main() {
  fmt.Println(add(42, 13))
}
package main

import "fmt"

func swap(x, y string) (string, string) {
  return y, x
}

func main() {
  a, b := swap("hello", "world")
  fmt.Println(a, b)
}

Variables

Declare variables

Use var

Use short variables declarations

package main

import "fmt"

var c, python, java bool

func main() {
  var i int
  fmt.Println(i, c, python, java)
}
package main

import "fmt"

func main() {
  var i, j int = 1, 2
  k := 3
  c, python, java := true, false, "no!"

  fmt.Println(i, j, k, c, python, java)
}

Convert type

package main

import (
    "fmt"
    "math"
)

func main() {
    var x, y int = 3, 4
    var f float64 = math.Sqrt(float64(x*x + y*y))
    var z uint = uint(f)
    fmt.Println(x, y, z)
}

Loop

Declare variables

For loop

while is spelled for in Go

package main

import "fmt"

func main() {
  sum := 0
  for i := 0; i < 10; i++ {
    sum += i
  }
  fmt.Println(sum)
}
package main

import "fmt"

func main() {
  sum := 1
  for sum < 1000 {
    sum += sum
  }
  fmt.Println(sum)
}

Defer

A defer statement defers the execution of a function until the surrounding function return

package main

import "fmt"

func main() {
    defer fmt.Println("world")

    fmt.Println("hello")
}

Pointer

Struct fields can be accessed through a struct pointer

package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

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

Array

Arrays cannot be resized

package main

import "fmt"

func main() {
    var a [2]string
    a[0] = "Hello"
    a[1] = "World"
    fmt.Println(a[0], a[1])
    fmt.Println(a)

    primes := [6]int{2, 3, 5, 7, 11, 13}
    fmt.Println(primes)
}

Slice

A dynamically-sized

Print 1 slice

Print 4 slices

package main

import "fmt"

func main() {
  primes := [6]int{2, 3, 5, 7, 11, 13}

  var s []int = primes[1:4]
  fmt.Println(s, primes)
}
package main

import "fmt"

func main() {
  a := make([]int, 5)
  printSlice("a", a)

  b := make([]int, 0, 5)
  printSlice("b", b)

  c := b[:2]
  printSlice("c", c)

  d := c[2:5]
  printSlice("d", d)
}

func printSlice(s string, x []int) {
  fmt.Printf("%s len=%d cap=%d %v\n",
    s, len(x), cap(x), x)
}

Range

The range form of the for loop iterates over a slice or map

package main

import "fmt"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
    for i, v := range pow {
        fmt.Printf("2**%d = %d\n", i, v)
    }
}

Method

Method is a function of struct

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := Vertex{3, 4}
    fmt.Println(v.Abs())
}

Interface

An interface type is defined as a set of method signatures

package main

import (
    "fmt"
    "math"
)

type Abser interface {
    Abs() float64
}

func main() {
    var a Abser
    f := MyFloat(-math.Sqrt2)
    v := Vertex{3, 4}

    a = f    // a MyFloat implements Abser
    a = &v // a *Vertex implements Abser

    // In the following line, v is a Vertex (not *Vertex)
    // and does NOT implement Abser.
    a = v

    fmt.Println(a.Abs())
}

type MyFloat float64

func (f MyFloat) Abs() float64 {
    if f < 0 {
        return float64(-f)
    }
    return float64(f)
}

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

Exercises:

After finished the basic syntax, you should do these exercises

Exercise 1

  • Objectives:
    • Understand how to query data from RMS database, using "database/sql" package
    • Can insert, update, delete data
    • Understand how to use Mux to receive an http request, and return an http response
  • Create a CRUD REST API with mux and My SQL, table users, with these fields fields id, username, email, phone, dateOfBirth, and methods GetAll, GetByID, Insert, Update, Delete

Exercise 2

  • Objectives:
    • Understand how to query data from Mongo
    • Can insert, update, delete data
    • Understand how to use gin to receive an http request, and return an http response
  • Create a CRUD REST API with gin and MongoDB: collection user, with these fields id, username, email, phone, dateOfBirth, and methods: GetAll, GetByID, Insert, Update, Delete

Exercise 3

  • Objectives:
    • Understand how to query data from RMS database, using gorm
    • Can insert, update, delete data
    • Understand how to use echo to receive an http request, and return an http response
  • Create a CRUD REST API with echo and gorm with My SQL: collection user, with these fields id, username, email, phone, dateOfBirth, and methods: GetAll, GetByID, Insert, Update, Delete

Script to create database for Exercise 1 and Exercise 3

-- script to create database for exercise 1, exercise 3
create table if not exists users (
  id varchar(40) not null,
  username varchar(120),
  email varchar(120),
  phone varchar(45),
  date_of_birth date,
  primary key (id)
);

insert into users (id, username, email, phone, date_of_birth) values ('ironman', 'tony.stark', 'tony.stark@gmail.com', '0987654321', '1963-03-25');
insert into users (id, username, email, phone, date_of_birth) values ('spiderman', 'peter.parker', 'peter.parker@gmail.com', '0987654321', '1962-08-25');
insert into users (id, username, email, phone, date_of_birth) values ('wolverine', 'james.howlett', 'james.howlett@gmail.com', '0987654321', '1974-11-16');

Real project samples

Layer Architecture Samples

Layer Architecture

Layer Architecture

Layer Architecture with standard features: config, health check, logging, middleware log tracing, data validation

Layer Architecture with standard features: config, health check, logging, middleware log tracing, data validation

  • To build a REST API to support
  • Apply 3 tier architecture: handler, service (business logic) and repository
    • Each layer is put in a separated package
  • Some standard features
  • A micro service with mux and SQL
  • At SQL layer, support "patch", using core-go/sql

Modular Samples

Modular

Architecture with standard features: config, health check, logging, middleware log tracing, data validation

Architecture with standard features: config, health check, logging, middleware log tracing, data validation

  • To build a REST API to support
  • 3 layers are put into the same package (handler, service (business logic) and repository)
  • Some standard features
  • A micro service with mux and SQL
  • At SQL layer, support "patch", using core-go/sql

Message Queue Samples

Flow to consume a message from a queue

Flow to consume a message

  • Consume a message from queue, then write the message to database (SQL, Mongo, Casandra, Dynamodb, Firestore, Elasticsearch)
  • Use core-go/mq
  • Support these message queues:
  • Support these databases
    • SQL
    • Mongo
    • Casandra
    • Dynamodb
    • Firestore
    • Elasticsearch
  • Consume a message from queue one by one
  • After the configured interval time (for example, 5 seconds) or reach the batch size (for example, reach 1000 messages), write all messages (1000 messages) to database (SQL, Mongo, Casandra, Dynamodb, Firestore, Elasticsearch)
Architecture

User Role Service

User and role management, with these features:

  • Authentication
    • Log in by LDAP
    • After logged in, get all privileges based on roles of that user
  • Authorization: Separate the "read" and "write" permissions for 1 role, using bitwise. For example:
    • 001 (1 in decimal) is "read" permission
    • 010 (2 in decimal) is "write" permission
    • 100 (4 in decimal) is "delete" permission
    • "read" and "write" permission will be "001 | 010 = 011" (011 is 3 in decimal)
  • Some other standard features

Low code

Components

Components

Commandline

export
  • input: database, project settings
  • output: metadata
generate
  • input: metadata, project templates
  • output: project (working application)

GUI

generator
  • GUI, include "export" and "generate"

Business View

Business View

Download

Output Samples

https://github.com/source-code-template

GO Layer Architecture Sample
nodejs Layer Architecture Sample
nodejs Modular Sample
nodejs Simple Modular Sample

Tutorials

SQL

NO SQL

Web Frameworks

Layer Architecture Samples

Modular Samples

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published