Skip to content

herrera-ignacio/go-in-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go In Practice

1. Noteworthy aspects of Go

  1. Multiple returns
  2. Named return values
  3. Read TCP Status
  4. HTTP Get
  5. Concurrent output
  6. Using channels
  7. Hello world
  8. Testing hello world
  9. Hello World web server

2. A solid foundation

  1. "Hello" CLI w/flag
  2. "Hello" CLI w/go-flags
  3. "Hello" CLI w/cli.go
  4. Count Up/Down w/cli.go
  5. Using JSON config
  6. Using YAML config
  7. Using INI config
  8. Using env variables
  9. Callback shutdown URL (anti-pattern)
  10. Graceful shutdown using manners
  11. Resolve URLs with handler functions
  12. Resolve URLs using path
  13. Resolve URLs using RegExp
  14. Faster routing
    1. github.com/julienschmidt/httprouter
    2. github.com/gorilla/mux
    3. github.com/bmizerany/pat

3. Concurrency

Objectives

  • Understanding Go's CSP-based concurrency model
  • Concurrent processing with goroutines
  • Locking and waiting with the sync package and buffered channels.
  • Communication between goroutines using channels
  • Strategically closing channels

Techniques

  1. Using goroutines closures
  2. Waiting for goroutines
  3. Locking with a mutex
  4. Using multiple channels
  5. Closing channels
  6. Locking with buffered channels

Examples

  1. Using a goroutine to run a task
  2. Anonymous functions & closures
  3. Gzip compression tool
  4. Gzip compression with wait group
  5. Word counter
    1. Word counter w/race condition
    2. Word counter w/locks
  6. Using multiple channels
  7. Pausing with Sleep and After
  8. Improper channel close
  9. Close from sender
  10. Close using a close channel

4. Handling errors and panics

Objectives

  • Understand Go's patterns for error handling
  • Using error variables and custom error types
  • Providing meaningful data with errors
  • Handling panics
  • Transforming panics into errors
  • Error handling on goroutines

Techniques

An error indicates that a particular task couldn't be completed successfully. A panic indicates that a severe event ocurred, probably as a result of a programmer error.

  1. Minimize the nils
  2. Custom error types
  3. Error variables
  4. Issuing panics
  5. Recovering from panics
  6. Trapping panics on goroutines

Examples

  1. Returning an error
  2. Relying on good error handling
  3. Parse error
  4. Error variables
  5. Error and panic
  6. Recovering from panics
  7. Handle panics on a goroutine

5. Debugging and testing

Objectives

  • Capturing debugging information
  • Using a logger (e.g., network)
  • Capturing stack traces
  • Writing unit tests.
  • Benchmarking and acceptance tests
  • Performing basic generative testing
  • Detecting race conditions

Techniques

  1. Logging to an arbitrary writer
  2. Logging to a network resource
  3. Handling back pressure in network logging
  4. Logging to the syslog
  5. Capturing stack traces
  6. Using interfaces for mocking or stubbing
  7. Verifying interfaces with canary tests
  8. Generative testing
  9. Benchmarking Go code
  10. Parallel benchmarks
  11. Detecting race conditions

Examples

  1. Logging to a file
  2. Network log client
  3. UDP-based logging
  4. Logging to syslog
  5. Logging to system log
  6. Capturing stack traces with runtime/debug
  7. Capturing stack traces with Stack function
  8. Hello test
  9. Generative test
  10. Benchmarking
  11. Two templates benchmark
  12. Parallel benchmarks

6. HTML and email template patterns

Objectives

  • Extending the functionality within templates with custom functions and pipping commands.
  • Caching & buffering templates
  • Template inheritance
  • Nesting templates
  • Mapping templates to objects, such as a user template for a user object, and rolling the templates up into a page-level output.
  • Generating email output with templates

Techniques

  1. Extending templates with functions
  2. Caching parsed templates
  3. Handling template execution failures
  4. Nested templates
  5. Template Inheritance
  6. Mapping data types to templates
  7. Generating email from templates

Example

  1. Simple HTML template
  2. Template functions
  3. Cache parsed templates
  4. Buffer templates for error handling
  5. Template inheritance
  6. Mapping data types to templates
  7. Email template

7. Serving and receiving assets and forms

Objectives

  • Uploading files to users from a Go server.
  • Go helper functions for quick and easy access to form submissions.
  • Form parsing.
  • Multipart form handling.

Techniques

  1. Serving subdirectories
  2. File server with custom error pages
  3. Caching file server
  4. Embedding files in a binary
  5. Serving from an alternative location
  6. Accessing multiple values for a form field
  7. Uploading a single file
  8. Uploading multiple files
  9. Verify uploaded file is allowed type
  10. Incrementally saving a file (mutlipart data)

Example

  1. http package file serving
  2. Serve file with custom handler
  3. Serve subdirectories with http.Dir handler
  4. Serve subdirectories with path handler
  5. File not found error handler
  6. Cache serving
  7. Embedding files in a binary
  8. Serving from alternative location
  9. Parsing a simple form from response
  10. Parsing multiple values from form
  11. A form with a single-value file-upload field
  12. Multiple file uploads

8. Working with web services

Objectives

  • Detecting network timeouts.
  • Resuming downloads when timeouts occur.
  • Parsing errors between API endpoints and client requestors.
  • Parsing JSON, even when you don't know the schema ahead of time.
  • Versioning REST APIs with url and content-type.

Techniques

  1. Detecting timeouts
  2. Timing out and resuming with HTTP
  3. Custom HTTP error passing
  4. Reading custom errors
  5. Parsing JSON without knowing the schema
  6. API version in the URL
  7. API version in content type

Examples

  1. A simple HTTP get
  2. Delete request with default http client
  3. Simple custom HTTP client
  4. Detecting timeouts and resuming with HTTP
  5. Passing an error over HTTP
  6. Custom JSON error response
  7. Convert HTTP response to an error
  8. Parsing JSON
  9. Parsing JSON with no schema
  10. API versioning in the URL
  11. API versioning in the content type
  12. Request API with content type versioning

9. Using the cloud

Objectives

  • Working with multiple cloud providers avoiding vendor lock-in.
  • Gathering information about the host.
  • Compiling to various operating systems and architectures.
  • Monitoring the Go runtime to detect issues and details about a running application.

Techniques

  1. Working with multiple cloud providers
  2. Cleanly handling cloud provider errors
  3. Gathering information on the host
  4. Detecting dependencies
  5. Cross-compiling
  6. Monitoring the Go runtime

Examples

  1. Simple file storage
  2. Handling cloud provider errors
  3. Gathering information on the host
  4. Detecting dependencies
  5. Monitoring the Go runtime

10. Communication between cloud services

Objectives

  • Communications in a microservice architecture.
  • Reusing connections to improve performance by avoiding repeated TCP slow-start, congestion-control ramp-ups and connection negotiations.
  • Faster JSON mashaling and unmarshaling that avoids extra time spent reflecting.
  • Communicating over RPC using gRPC.

Techniques

  1. Reusing connections
  2. Faster JSON marshal and unmarshal
  3. Using protocol buffers
  4. Communicating over RPC with protocol buffers

Examples

  1. Faster JSON marshal and unmarshal
  2. Protocol buffer file
  3. Protocol buffer server
  4. Protocol buffer client
  5. RPC proto file
  6. gRPC server

11. Reflection and code generation

Objectives

  • Use kinds to identify critical details about types.
  • Determine at runtime whether a type implements an interface.
  • Access struct fields at runtime.
  • Work with annotations.
  • Parse tags within struct annotations.
  • Write marshal and unmarshal functions.
  • Use go generate.
  • Write Go templates that generate Go code.

Techniques

  1. Switching based on type and kind
  2. Discovering whether a value implements an interface
  3. Accessing fields on a struct
  4. Processing tags on a struct
  5. Generating code with go generate

Examples

  1. Sum with type switch
  2. Sum with Kind switch
  3. Checking and converting a type
  4. Checking for an interface
  5. Recursively examining a value
  6. Simple JSON struct
  7. Processing tags
  8. The queue template & desired output

About

Collection of coding examples from "Go In Practice"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages