GO 4 MODULE
GO 4 MODULE
Today, we look into the best libraries and packages to make working with
Go even easier.
1. GORM
GORM is the most popular DB libraries for Go. It is a full-featured
systems. GORM provides SQL builders, RAW SQL, auto migration tools,
Extendable plugins for customization. All the features in GORM come with its
own tests so that developers can easily try something new without borking the
whole system.
2. Gen
Gen tool generates code for you. The tools help to generate type aware code in
particular which tries to alleviate the gap of lacking templates in Golang. With
no runtime magic, annotates your types with a special comment and generate
source files.
3. Goose
Managing the schema is the primary task when working on relational
4. cli
cli is a simple and fast package for building command-line apps for Golang. It
allows the developers to develop their own expressive command-line apps. cli
is used for creating flags, bash-completion routines and generates help texts.
cli is fast, fun and playful when you code for an API.
5. Go Kit
Go Kit is GitHub’s most popular standard library for Go related Microservice.
This library offers specialized support for microservices. Go kit addresses the
6. Vegeta
Vegeta is a tool used for HTTP load testing. This versatile tool was specially
designed for testing HTTP services with a constant request rate. It works
efficiently on analyzing the weak points of the program. Vegeta is one of the
presumably named after the Saiyan prince for its attack both targeted and
7. Authboss
Authboss is a library for Go and also a modular web authentication system.
integrate it, even without web frameworks and use it for fixing the bugs.
8. Glide
Glide is a package manager for Go. Glides offer help to manage each program
Also Read
9. Ginkgo
Ginkgo is a Behaviour Driven Development (BDD) testing framework. It
allows you to write your tests in a syntax that resembles the English language,
which makes it easy for people with less or no technical knowledge to review
the tests or outputs which match the requirements of the business. It has a
stylish test specification to integrate with Go’s inbuilt testing package and is
10. Docker
Docker is a service product that is used for OS-level virtualization to deliver
most of the developers are not aware that Docker is implemented in Go.
11. Kubernetes
Kubernetes is an open-source container orchestration platform for
Kubernetes is flexible and comes with extendable and customizable plugins for
use.
12. Fuzzy
Fuzzy is a dependency-free Go library, it is used to match fuzzy strings. It is
optimized for file names and code symbols. It provides speed and intuitive
its own internal logic. Fuzzy is Unicode aware and can parse IntelliJ IDEA,
13. mgo
mgo is a library for Golang. It is a MongoDB driver that implements rich
functionalities under a simple API standard for Go idioms. With mgo, you get
superior performance with Flexible serialization. It has the support of GridFS
14. NSQ
NSQ is a robust distributed queue. It is primarily used for a building block for
large scale distributed systems. It is a simple TCP protocol that supports client
style message routing. There are few dependencies that are easy to deploy with
the default configuration. There is no need for the client library to publish the
15. Etcd
Etcd is a reliable distributed key-Value store. It is a simple, well defines
user-facing API. The server can be easily implemented and the Goe client
interacts with it through gRPC. It is highly secure with automatic TLS with
defines a search pattern that is used for matching specific text. In Golang,
there’s a built-in package for regular expressions, called the regexp package
reports whether the string passed as a parameter contains any match of the
Syntax:
// in-built function
package main
import (
"fmt"
"regexp"
func main() {
// is to be found
str := "geeksforgeeks"
// is unsuccessful
str2 := "ComputerScience"
OUTPUT:
`geek(s
https://www.geeksforgeeks.org/what-is-regex-in-golang/
The `Lock()` method acquires the lock, and the `Unlock()` method releases it.
Using a mutex helps prevent race conditions and ensures the correctness of
package main
import (
"fmt"
"sync"
"time"
var (
counter = 0
mutex sync.Mutex
func increment() {
func main() {
go increment()
}
time.Sleep(time.Second)
fmt.Println("Counter:", counter)
}
https://www.digitalocean.com/community/tutorials/understandin
g-package-visibility-in-go
but its design is simpler. The comments read by godoc are not language
constructs (as with Docstring) nor must they have their own
good comments, the sort you would want to read even if godoc didn’t
exist.
its declaration, with no intervening blank line. Godoc will then present
that comment as text alongside the item it documents. For example, this
// Fprint formats using the default formats for its operands and writes to
w.
Notice this comment is a complete sentence that begins with the name
to UNIX man pages, and makes it read better when tools truncate it for
brief description:
// collections.
package sort
They can also be detailed like the gob package’s overview. That package
file, doc.go, which contains only those comments and a package clause.
When writing a package comment of any size, keep in mind that its first
that begin with the word "BUG(who)” are recognized as known bugs,
“who” part should be the user name of someone who could provide
more information. For example, this is a known issue from the bytes
package:
// BUG(r): The rule Title uses for word boundaries does not handle
comments to HTML:
necessary.
Note that none of these rules requires you to do anything out of the
ordinary.
In fact, the best thing about godoc’s minimal approach is how easy it is
Structs
Definition of a struct:
A structure or struct in Golang is a user-defined type that allows to
address has a name, street, city, state, Pincode. It makes sense to group
Declaring a structure:
name string
street string
city string
state string
Pincode int
}
In the above, the type keyword introduces a new type. It is followed by the
name of the type (Address) and the keyword struct to illustrate that we’re
defining a struct. The struct contains a list of various fields inside the curly
Note: We can also make them compact by combining the various fields of the
Pincode int
var a Address
The above code creates a variable of a type Address which is by default set
to zero. For a struct, zero means all the fields are set to their corresponding
zero value. So the fields name, street, city, state are set to “”, and Pincode is
set to 0. You can also initialize a variable of a struct type using a struct literal
as shown below:
var a = Address{"Akshay", "PremNagar", "Dehradun",
"Uttarakhand", 252636}
Note:
● Always pass the field values in the same order in which they are
declared in the struct. Also, you can’t initialize only a subset of fields
● Go also supports the name: value syntax for initializing a struct (the
order of fields is irrelevant when using this syntax). And this allows
you to initialize only a subset of fields. All the uninitialized fields are
Pincode:252636} //city:””
package main
import "fmt"
Name string
city string
Pincode int
func main() {
var a Address
fmt.Println(a)
// Declaring and initializing a
// initializing a struct
Pincode: 277001}
a3 := Address{Name: "Delhi"}
To access individual fields of a struct you have to use dot (.) operator.
Ex:
package main
import "fmt"
// Main Function
func main() {
// to a struct field
c.Color = "Black"
// Displaying the result
fmt.Println("Car: ", c)
Output:
https://refactoring.guru/design-patterns/factory-method/go/example
Struct Tags:
Go struct tags are annotations that appear after the type in a Go struct
declaration. Each tag is composed of short strings associated with some
corresponding value.
A struct tag looks like this, with the tag offset with backtick ` characters:
Other Go code is then capable of examining these structs and extracting the
values assigned to specific keys it requests. Struct tags have no effect on the
operation of your code without additional code that examines them.
Try this example to see what struct tags look like, and that without code from
another package, they will have no effect.
package main
import "fmt"
func main() {
u := &User{
Name: "Sammy",
fmt.Println(u)
Output