kspine / go-recipes

A collection of Golang recipes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.
  • 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.
    • Deadline - Timeout using context.WithDeadline function.
  • 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.
  • 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
      • 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.
  • 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.
  • 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.
    • 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.
  • 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.

About

A collection of Golang recipes.

License:MIT License


Languages

Language:Go 82.1%Language:Makefile 13.2%Language:HTML 3.7%Language:JavaScript 1.0%