I started this project as I was learning Go. Over time, it evolved into a handy reference for anyone to look up Go recipes and design patterns.
Here is a collection of examples:
- CLI - Setting commands, arguments, and flags when building a CLI program in Go.
- Cloud - Connecting to public Cloud API
- S3 - List a S3 bucket using aws-sdk-go package.
- Collection - Go map, array, and slice.
- Array passing - An array is passed as a value to a function.
- Map - Basic map operations.
- Slice - Basic slice operations.
- Sort - Sort a slice of custom type elements.
- Concurrency - Go concurrency.
- Channel
- Basics - Basic channel examples.
- Buffered vs unbuffered channel - Differences between buffered and unbuffered channels.
- Channel passing - Message passing between 2 goroutines using a channel.
- WaitGroup - Using
sync.WaitGroup
to synchronize multiple concurrent goroutines. - Limit number of goroutines - Limit the number of concurrent goroutines that can be executed using buffered channel.
- Channel
- Context - The
context
package. - Cookie - Introduction to cookies in Go.
- Dependency injection - Different ways of implementing dependency injection using the
dig
andwire
frameworks, and without a third-party framework. - Enum - Implement enum in Go.
- Error wrapping - Go error wrapping.
- Fake vs mock in unit testing - The use of fake and mock in Go unit testing.
- Generics - Go Generics.
- Custom type - Using a custom type as a type parameter.
- Linked list - Generic-based linked list.
- Type constraints -Type constraint basics.
- Type parameters - Type parameter basics.
- GraphQL - GraphQL in Go.
- GraphQL client - A simple GraphQL client using the
machinebox/graphql
package. - GraphQL server - A simple GraphQL server using the
99designs/gqlgen
package.
- GraphQL client - A simple GraphQL client using the
- gRPC - GRPC examples.
- Hello world - A simple gRPC server-client setup.
- gRPC test - Unit testing a gRPC server.
- HTTP - HTTP server.
- Client - A simple HTTP client.
- Server
- CORS - How to implement cors on a web server using the gin and gorilla frameworks.
- Form - Handles a form submission (object binding and validation) using the gin framework.
- JWT - Handles a simple web form submission and a simple authentication using JWT using the
dgrijalva/jwt-go
package. - Static web server - Implements a simple http server serving static content.
- Nexjs - Serve next.js client-side application from a Go server.
- I/O - Input/output operations.
- Basic read - Different ways to read.
- File reader - Different ways of reading from a file: incrementally and all-at-once.
- File writer - Writing to a file.
- Mutli-read - Reading from a source multiple times.
- Pipe - Establish a pipeline for writing content on one end of the pipeline to reading on the end.
- Long polling - A simple long-polling implementation.
- MongoDB - Working with MongoDB using the official Go driver.
- Basic - Basic operations with Mongo.
- Expiring data - Remove expired data using MongoDB TTL indexing.
- Mongo operations - Run Mongo commands and other queries.
- Pre-defined schema - An example similar to the basic mongo recipe, but implemented using a pre-defined schema.
- Pubsub - Pubsub examples.
- Kafka - A simple pubsub application using kafka.
- QRCode - QRCode examples.
- QRCode Reader - QRCode reader.
- Patterns - Common design patterns in Go.
- Functional programming - Functional programming.
- Inheritance - Inheritance through composition.
- Random - Generating random data in Go.
- Redis - recipes for working with Redis using the
mediocregopher/radix
andgo-redis/redis
drivers.- Counter - Global atomic counter that showcases basic operations in Redis.
- Sessions - Ephemeral sessions in Redis by setting up Redis to remove expired content using the Go-Redis and Radix.
- Authentication - Handling Redis authentication.
- Reflection - Go runtime reflection.
- Basics - Behaviors of reflection objects on different underlying values.
- Deep equal - Go (deep) equality operation using
reflect.DeepEqual
to compare 2 values. - Merge map to struct - Merge Matching Fields from Map to Struct.
- Merge map to struct recursively - Same as the above merge map to struct with support for nested fields.
- Print fields - Print out the fields of a custom struct type.
- Set value - Set a value on reflect.Value object.
- Regular expression - Regular expression basics.
- Retry - Ways to implement retry in Go.
- Standard - Simple retry implemented using Go, no third-party package.
- Retry package - Retry using the
flowchartsman/retry
package.
- Scheduler - Ways to schedule code to run periodically in the background.
- Serialization - Go serialization.
- System - System.
- Interrupt via channel - Get notify of a system interrupt via channels.
- SQL - Connecting to a relational database.
- Ephemeral SQL data model - SQL garbage collector that removes expired records in sql database.
- SQL - Connect to Postgres using the
database/sql
package. - SQLX and Squirrel - Connect to postgres using sqlx and squirrel packages.
- SQLite - Connect to a SQLite database.
- String - Strings and characters in Go.
- Count iterate - Count and iterate over a string by rune and byte.
- Unicode - Unicode basics.
- Template - Go template.
- HTML template - Server-side web content rendering using the
html/template
package. - Layouts - Create reusable templates (aka layouts).
- HTML template - Server-side web content rendering using the
- Testing - Go testing.
- T.Helper - What is T.Helper() and what it does.
- Time - Datetime in Go.
- Timer - Go timer patterns.
- Timed repetition - Messages are printed every few seconds.
- TLS - TLS support in Go.
- Client-MySQL - Connect to MySQL using TLS and generate self-signed ca/server/client certs/tls using mysql tls gen script.
- Client-Postgres - Connect to Postgres using TLS and generate self-signed ca/server/client certs/tls using openssl.
- URL - URL in Go.
- Query parameters - Construct query parameters with an URL object.
- Type check - Type checking at runtime.
- Validation - Sample code for the
go-playground/validator
package. - WebAssembly - A simple web assembly that prints "hello world" in the browser console.
- Webhook - The client will push a random event message to a webhook endpoint on the sever every 5 seconds.