Cloud-oriented Life

Cloud Native Technology Improves Lives

GORM

The fantastic ORM library for Golang aims to be developer friendly.

Overview

  • Full-Featured ORM

  • Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance)

  • Hooks (Before/After Create/Save/Update/Delete/Find)

  • Eager loading with Preload, Joins

  • Transactions, Nested Transactions, Save Point, RollbackTo to Saved Point

  • Context, Prepared Statement Mode, DryRun Mode

  • Batch Insert, FindInBatches, Find/Create with Map, CRUD with SQL Expr and Context Valuer

  • SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints, Named Argument, SubQuery

  • Composite Primary Key, Indexes, Constraints

  • Auto Migrations

  • Logger

  • Extendable, flexible plugin API: Database Resolver (Multiple Databases, Read/Write Splitting) / Prometheus…

  • Every feature comes with tests

  • Developer Friendly

Installation

1
2
3
$ go get -u gorm.io/gorm

$ go get -u gorm.io/driver/sqlite

Quick Start

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
"gorm.io/gorm"
"gorm.io/driver/sqlite"
)

type Product struct {
gorm.Model
Code string
Price uint
}

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}

// Migrate the schema
db.AutoMigrate(&Product{})

// Create
db.Create(&Product{Code: "D42", Price: 100})

// Read
var product Product
db.First(&product, 1) // find product with integer primary key
db.First(&product, "code = ?", "D42") // find product with code D42

// Update - update product's price to 200
db.Model(&product).Update("Price", 200)
// Update - update multiple fields
db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

// Delete - delete product
db.Delete(&product, 1)
}

References

[1] GORM Guides | GORM - The fantastic ORM library for Golang, aims to be developer friendly. - https://gorm.io/docs/

[2] GORM - The fantastic ORM library for Golang, aims to be developer friendly. - https://gorm.io/

[3] go-gorm/gorm: The fantastic ORM library for Golang, aims to be developer friendly - https://github.com/go-gorm/gorm

[4] gorm · pkg.go.dev - https://pkg.go.dev/gorm.io/gorm

logrus

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

Logrus is in maintenance-mode. We will not be introducing new features. It’s simply too hard to do in a way that won’t break many people’s projects, which is the last thing you want from your Logging library (again…).

Read more »

Cobra

Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.

Cobra is used in many Go projects such as [Kubernetes - https://kubernetes.io/], Hugo - https://gohugo.io/, and Github CLI - https://github.com/cli/cli to name a few. This list - https://github.com/spf13/cobra/blob/master/projects_using_cobra.md contains a more extensive list of projects using Cobra.

Read more »

gofakeit

gofakeit is a Random data generator written in go.

It can fake Random data about File, Person, Generator, Auth, Address, Game, Beer, Car, Words, Foods, Misc, Colors, Internat, Date/Time, Payment, Company, Hacker, Hipster, App, Animal, Emoji, Language, Number, String, and custom Function, etc.

Read more »

sync.Once

Once is an object that will perform exactly one action.

.Do calls the function f if and only if .Do is being called for the first time for this instance of Once. In other words, given var once Once if once.Do(f) is called multiple times, only the first call will invoke f,
even if f has a different value in each invocation.

A new instance of Once is required for each function to execute.

Read more »

sync.WaitGroup

WaitGroup is used to wait for all the goroutines launched here to finish since the main function actually terminates before the goroutine gets a chance to execute by defatult.

WaitGroups essentially allow us to tackle this problem by blocking until any goroutines within that WaitGroup have successfully executed.

We first call .Add(n) on our WaitGroup to set the number n of goroutines we want to wait for, and subsequently, we call .Done() within any goroutine to signal the end of its’ execution.

Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.

Read more »

log

Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined ‘standard’ Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.

Read more »
0%