Skip to content

cybersamx/go-recipes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collection of Golang Recipes

Go Report Card

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.

Examples

Here is a collection of examples:

  • CLI - Setting commands, arguments, and flags when building a CLI program in Go.
    • cobra - Implement a cli program using the cobra package.
    • viper - Passing configurations to a cli program using cli arguments, environment variables, and text file.
  • 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.
  • Context - The context package.
    • Cancel - Cancel contexts.
    • Timeout - An example of how context.WithTimeout() works and be used for handling long-running operations.
  • Cookie - Introduction to cookies in Go.
  • Dependency injection - Different ways of implementing dependency injection using the dig and wire 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.
  • 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.
  • gRPC - GRPC examples.
  • 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.
  • Pubsub - Pubsub examples.
    • Kafka - A simple pubsub application using kafka.
  • QRCode - QRCode examples.
  • Patterns - Common design patterns in Go.
  • Random - Generating random data in Go.
  • Redis - recipes for working with Redis using the mediocregopher/radix and go-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.
    • CSV - Write and read csv files.
    • JSON - Basic json serialization using json.MarshalJSON() and json.UnmarshalJSON() functions.
  • System - System.
  • 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.
  • Template - Go template.
    • HTML template - Server-side web content rendering using the html/template package.
    • Layouts - Create reusable templates (aka layouts).
  • Testing - Go testing.
    • T.Helper - What is T.Helper() and what it does.
  • Time - Datetime in Go.
    • Parse - Parses a date string to a Go struct value.
    • Print - Prints a date value to different formats.
  • Timer - Go timer patterns.
  • 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.
  • 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.