List of Listings

Chapter 1. Getting into Go

Listing 1.1. Multiple returns: returns.go

Listing 1.2. Named return values: returns2.go

Listing 1.3. Read TCP status: read_status.go

Listing 1.4. HTTP GET: http_get.go

Listing 1.5. Concurrent output

Listing 1.6. Printing concurrently

Listing 1.7. Using channels: channel.go

Listing 1.8. Channel output

Listing 1.9. Single package import

Listing 1.10. Hello World: hello.go

Listing 1.11. Hello World test: hello_test.go

Listing 1.12. Running go test

Listing 1.13. Running go test failure

Listing 1.14. Testing with code coverage

Listing 1.15. Workspace layout

Listing 1.16. Hello World web server: inigo.go

Listing 1.17. Running inigo.go

Listing 1.18. Building inigo.go

Chapter 2. A solid foundation

Listing 2.1. Hello World CLI using flag package

Listing 2.2. Source of Hello World using flag package: flag_cli.go

Listing 2.3. Custom flag help text

Listing 2.4. flag package flag parsing

Listing 2.5. Using go-flags

Listing 2.6. Hello World CLI: hello_cli.go

Listing 2.7. Hello World CLI: hello_cli.go

Listing 2.8. Count up and down: count_cli.go

Listing 2.9. Parsing a JSON configuration file: json_config.go

Listing 2.10. Parsing a YAML configuration file: yaml_config.go

Listing 2.11. Parsing an INI configuration file: ini_config.go

Listing 2.12. Environment variable–based configuration: env_config.go

Listing 2.13. Start and stop applications with upstart

Listing 2.14. Callback shutdown URL: callback_shutdown.go

Listing 2.15. Graceful shutdown using manners: manners_shutdown.go

Listing 2.16. Multiple handler functions: multiple_handlers.go

Listing 2.17. Resolve URLs using path package: path_handlers.go

Listing 2.18. Resolve URLs using regular expressions: regex_handlers.go

Chapter 3. Concurrency in Go

Listing 3.1. Using a goroutine to run a task

Listing 3.2. An anonymous goroutine

Listing 3.3. Simple Gzip compression tool

Listing 3.4. Compressing files in parallel with a wait group

Listing 3.5. Word counter with race condition

Listing 3.6. Word counter with locks

Listing 3.7. Using multiple channels

Listing 3.8. Pausing with Sleep and After

Listing 3.9. Improper channel close

Listing 3.10. Close from sender

Listing 3.11. Using a close channel

Listing 3.12. Simple locking with channels

Chapter 4. Handling errors and panics

Listing 4.1. Returning an error

Listing 4.2. Handling an error

Listing 4.3. Returning useful data with an error

Listing 4.4. Relying on good error handling

Listing 4.5. Documenting returns under error conditions

Listing 4.6. The error interface

Listing 4.7. Parse error

Listing 4.8. Handling two different errors

Listing 4.9. Error and panic

Listing 4.10. Panic with nil

Listing 4.11. A proper panic

Listing 4.12. Simple defer

Listing 4.13. Recovering from a panic

Listing 4.14. Scope for deferred closures

Listing 4.15. msg out of scope

Listing 4.16. Cleanup

Listing 4.17. An echo server

Listing 4.18. Panic in the response

Listing 4.19. Handle panics on a goroutine

Listing 4.20. A small HTTP server

Listing 4.21. A panicky handler

Listing 4.22. safely.Go

Listing 4.23. Using safely.Go to trap panics

Chapter 5. Debugging and testing

Listing 5.1. Simple log usage

Listing 5.2. Logging to a file

Listing 5.3. Network log client

Listing 5.4. UDP-based logging

Listing 5.5. A logger directed to syslog

Listing 5.6. Logging to the system log

Listing 5.7. Print stack to Standard Output

Listing 5.8. Using the Stack function

Listing 5.9. A Simple hello

Listing 5.10. A hello test

Listing 5.11. The stringer interface

Listing 5.12. The message struct

Listing 5.13. Use an interface

Listing 5.14. Testing with a mock

Listing 5.15. MyWriter

Listing 5.16. Asserting a writer

Listing 5.17. Canary test of MyWriter

Listing 5.18. A padding function

Listing 5.19. Simple pad unit test

Listing 5.20. Generative test for pad

Listing 5.21. Benchmark template compile and run

Listing 5.22. Two template benchmarks

Listing 5.23. Parallel benchmarking

Listing 5.24. Benchmarks and race conditions

Chapter 6. HTML and email template patterns

Listing 6.1. A simple HTML template: simple.html

Listing 6.2. Using a simple HTML template: simple_template.go

Listing 6.3. Add template functions: date_command.go

Listing 6.4. Caching a parsed template: cache_template.go

Listing 6.5. Buffering a template response: buffered_template.go

Listing 6.6. Index template including head template: index.html

Listing 6.7. Head template included in the index: head.html

Listing 6.8. Using the nested templates: nested_templates.go

Listing 6.9. A base template to inherit from: base.html

Listing 6.10. Inheriting required sections: user.html

Listing 6.11. Inheriting with optional section: page.html

Listing 6.12. Using template inheritance: inherit.go

Listing 6.13. A Quote object template: quote.html

Listing 6.14. A generic page wrapper: index.html

Listing 6.15. Bringing the templates together: object_templates.go

Listing 6.16. Send email from a template: email.go

Chapter 7. Serving and receiving assets and forms

Listing 7.1. http package file serving: file_serving.go

Listing 7.2. Serve file with custom handler: servefile.go

Listing 7.3. Serving a subdirectory

Listing 7.4. Using path package path resolution

Listing 7.5. Custom file server error pages: file_not_found.go

Listing 7.6. Load and serve static files from memory: cache_serving.go

Listing 7.7. Embedding files in binaries with go.rice: embedded_files.go

Listing 7.8. Templates as embedded files

Listing 7.9. Passing a URL location to a template

Listing 7.10. Parsing a simple form response

Listing 7.11. Parsing a form with multiple values for a field

Listing 7.12. A form with a single-value file-upload field

Listing 7.13. Handle a single file upload

Listing 7.14. A form with a multiple value file-upload field

Listing 7.15. Process file form field with multiple files

Listing 7.16. HTML form containing a file and text field

Listing 7.17. Incrementally save uploaded files

Chapter 8. Working with web services

Listing 8.1. A simple HTTP get

Listing 8.2. DELETE request with default HTTP client

Listing 8.3. A simple custom HTTP client

Listing 8.4. Detect a network timeout from error

Listing 8.5. Download with retries

Listing 8.6. Passing an error over HTTP

Listing 8.7. Custom JSON error response

Listing 8.8. Convert HTTP response to an error

Listing 8.9. A simple custom JSON-parsing example

Listing 8.10. Parse JSON into an interface{}

Listing 8.11. Walk arbitrary JSON

Listing 8.12. Register the API path including a version

Listing 8.13. Pass the API version in the content type

Chapter 9. Using the cloud

Listing 9.1. Interface for cloud functionality

Listing 9.2. Simple implementation of cloud file storage

Listing 9.3. Example using a cloud provider interface

Listing 9.4. Errors to go with cloud functionality interface

Listing 9.5. Adding errors to file load method

Listing 9.6. Look up the host’s IP addresses via the hostname

Listing 9.7. Function to check whether the application is available

Listing 9.8. Cross-compile an application with gox

Listing 9.9. Monitor an application’s runtime

Chapter 10. Communication between cloud services

Listing 10.2. Failing to close an HTTP response body

Listing 10.3. Using and closing the HTTP response quickly

Listing 10.4. A struct annotated for codec

Listing 10.5. Encode an instance to JSON with codec

Listing 10.6. Decode JSON into an instance of a type

Listing 10.7. Protocol buffer file

Listing 10.8. Protocol buffer server setup

Listing 10.9. Protocol buffer server handler

Listing 10.10. Protocol buffer client

Listing 10.11. Define messages and RPC calls with a protocol buffer

Listing 10.12. gRPC server responding to requests

Listing 10.13. Request to a gRPC server with protocol buffers

Chapter 11. Reflection and code generation

Listing 11.1. Sum with type switch

Listing 11.2. Type switch with extra type

Listing 11.3. A Kind switch

Listing 11.4. Checking and converting a type

Listing 11.5. Determine whether a type implements an interface

Listing 11.6. Types to examine

Listing 11.7. Recursively examining a value

Listing 11.8. Simple JSON struct

Listing 11.9. A variety of annotations

Listing 11.10. A bare Processes struct

Listing 11.11. The Processes struct with annotations

Listing 11.12. The marshal and unmarshal pattern

Listing 11.13. Processes and main()

Listing 11.14. The Marshal function

Listing 11.15. The Unmarshal function

Listing 11.16. The setField helper function

Listing 11.17. A trivial generator

Listing 11.18. Simple queue

Listing 11.19. The queue template

Listing 11.20. The main queue generator

Listing 11.21. Using the generator

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.147.193