- To learn the basic syntax, you can start at https://go.dev/tour
- View online training slide at here
- Download training slide at here
- Every Go program is made up of packages
- Start running in package main
- Run here https://go.dev/tour/welcome/1
- In this sample, use “fmt” package to print a string to console
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}
Support to return multiple values
- Exercise 1: write a function, to add 2 integer numbers
- Run here https://go.dev/tour/basics/5
- Exercise 2: write a function, to swap 2 strings
- Run here https://go.dev/tour/basics/6
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)
} |
Declare variables
- Exercise 1: Use “var” to declare 3 variables
- Run here https://go.dev/tour/basics/8
- Exercise 2: Use short variables declarations
- Run here https://go.dev/tour/basics/10
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)
} |
- Use expression T(v) to convert some number
- Run here https://go.dev/tour/basics/13
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)
}
Declare variables
- Exercise 1: Total from 1 to 10
- Run here https://go.dev/tour/flowcontrol/1
- Exercise 2: Total from 1 to 10, with "while" loop logic
- Run here https://go.dev/tour/flowcontrol/3
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)
} |
A defer statement defers the execution of a function until the surrounding function return
- Exercise: Print “hello world” with defer
- Run here https://go.dev/tour/flowcontrol/12
package main
import "fmt"
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
Struct fields can be accessed through a struct pointer
- Exercise: Access a struct with a struct pointer
- Run here https://go.dev/tour/moretypes/4
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)
}
Arrays cannot be resized
- Exercise: Create arrays with string & int
- Run here https://go.dev/tour/moretypes/6
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)
}
A dynamically-sized
- Exercise 1: Create a slice from an array
- Run here https://go.dev/tour/moretypes/7
- Exercise 2: Create a slice from an array
- Run here https://go.dev/tour/moretypes/13
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)
} |
The range form of the for loop iterates over a slice or map
- Exercise: Loop a slice and print 2 powers
- Run here https://go.dev/tour/moretypes/16
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 is a function of struct
- Exercise: Create a method, with struct receiver
- Run here https://go.dev/tour/methods/1
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())
}
An interface type is defined as a set of method signatures
- Exercise: Create a method, with struct receiver
- Run here https://go.dev/tour/methods/9
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)
}
After finished the basic syntax, you should do these exercises
- 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
- Refer to go-sql-tutorial
- 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
- Refer to go-mongo-tutorial and go-gin-sql-tutorial
- Objectives:
- 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
- Refer to gorm-tutorial and go-echo-sql-tutorial
-- 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', '[email protected]', '0987654321', '1963-03-25');
insert into users (id, username, email, phone, date_of_birth) values ('spiderman', 'peter.parker', '[email protected]', '0987654321', '1962-08-25');
insert into users (id, username, email, phone, date_of_birth) values ('wolverine', 'james.howlett', '[email protected]', '0987654321', '1974-11-16');
Layer Architecture with standard features: config, health check, logging, middleware log tracing, data validation
- To build a REST API to support
- search, get by ID, create, update, delete
- support "patch" method, using core-go/service
- support "search" method, using core-go/search
- Apply 3 tier architecture: handler, service (business logic) and repository
- Each layer is put in a separated package
- Some standard features
- config: load config from yaml files
- health check: to check health of SQL
- logging: can use logrus or zap to log, support to switch between logrus or zap
- log tracing by at the middleware the http request and http response
- A micro service with mux and SQL
- At SQL layer, support "patch", using core-go/sql
- A micro service with mux and MongoDB
- At mongo layer, support "patch", using core-go/mongo
Architecture with standard features: config, health check, logging, middleware log tracing, data validation
- To build a REST API to support
- search, get by ID, create, update, delete
- support "patch" method, using core-go/service
- support "search" method, using core-go/search
- 3 layers are put into the same package (handler, service (business logic) and repository)
- Some standard features
- config: load config from yaml files
- health check: to check health of SQL
- logging: can use logrus or zap to log, support to switch between logrus or zap
- log tracing by at the middleware the http request and http response
- A micro service with mux and SQL
- At SQL layer, support "patch", using core-go/sql
- A micro service with mux and MongoDB
- At mongo layer, support "patch", using core-go/mongo
- 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:
- Amazon Simple Queue Service (SQS) at sqs
- Google Cloud Pub/Sub at pubsub
- Kafka: at segmentio/kafka-go, Shopify/sarama and confluent
- NATS at nats
- Active MQ at amq
- RabbitMQ at rabbitmq
- IBM MQ at ibm-mq
- 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)
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
- config: load config from yaml files
- health check: to check health of SQL
- logging: can use logrus or zap to log, support to switch between logrus or zap
- log tracing by at the middleware the http request and http response
- input: database, project settings
- output: metadata
- input: metadata, project templates
- output: project (working application)
- GUI, include "export" and "generate"
- https://github.com/lowcode-tech/windows
- https://github.com/lowcode-tech/mac
- https://github.com/lowcode-tech/linux
https://github.com/source-code-template
- https://github.com/source-code-template/go-sql-sample
- https://github.com/source-code-template/go-mongo-sample
- https://github.com/source-code-template/mongo-layer-architecture-sample
- https://github.com/source-code-template/sql-layer-architecture-sample
- https://github.com/source-code-template/mongo-layer-architecture-sample
- https://github.com/source-code-template/sql-layer-architecture-sample
- https://github.com/source-code-template/mongo-simple-modular-sample
- https://github.com/source-code-template/sql-simple-modular-sample