Go Standard Library Cookbook

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Google's Golang will be the next talk of the town, with amazing features and a powerful library. This book will gear you up for using golang by taking you through recipes that will teach you how to leverage the standard library to implement a particular solution. This will enable Go developers to take advantage of using a rock-solid standard library instead of third-party frameworks. The book begins by exploring the functionalities available for interaction between the environment and the operating system. We will explore common string operations, date/time manipulations, and numerical problems. We'll then move on to working with the database, accessing the filesystem, and performing I/O operations. From a networking perspective, we will touch on client and server-side solutions. The basics of concurrency are also covered, before we wrap up with a few tips and tricks. By the end of the book, you will have a good overview of the features of the Golang standard library and what you can achieve with them. Ultimately, you will be proficient in implementing solutions with powerful standard libraries.

Author(s): Radomír Sohlich
Publisher: Packt Publishing
Year: 2018

Language: English
Pages: 311
City: Birmingham

Table of Contents
Preface
Chapter 1: Interacting with the Environment
Introduction
Retrieving the Golang version
Getting ready
How to do it...
How it works...
Accessing program arguments
How to do it...
How it works...
There's more…
Creating a program interface with the flag package
How to do it...
How it works…
There's more…
Getting and setting environment variables with default values
How to do it…
How it works…
Retrieving the current working directory
How to do it...
How it works…
Getting the current process PID
How to do it…
How it works…
Handling operating system signals
How to do it…
How it works…
Calling an external process
Getting ready
How to do it…
How it works…
See also
Retrieving child process information
Getting ready
How to do it…
How it works…
See also
Reading/writing from the child process
Getting ready
How to do it…
How it works…
Shutting down the application gracefully
How to do it…
How it works…
See also
File configuration with functional options
How to do it...
How it works...
Chapter 2: Strings and Things
Introduction
Finding the substring in a string
How to do it...
How it works...
See also
Breaking the string into words
How to do it...
How it works...
There's more...
Joining the string slice with a separator
How to do it...
How it works...
There's more...
Concatenating a string with writer
How to do it...
How it works...
There's more...
Aligning text with tabwriter
How to do it...
How it works...
Replacing part of the string
How to do it...
How it works...
There's more...
Finding the substring in text by the regex pattern
How to do it...
How it works...
See also
Decoding a string from the non-Unicode charset
How to do it...
How it works...
Controlling case
How to do it...
How it works...
Parsing comma-separated data
How to do it...
How it works...
Managing whitespace in a string
How to do it...
How it works...
See also
Indenting a text document
How to do it...
How it works...
See also
Chapter 3: Dealing with Numbers
Introduction
Converting strings to numbers
How to do it...
How it works...
Comparing floating-point numbers
How to do it...
How it works...
Rounding floating-point numbers
How to do it...
How it works...
Floating-point arithmetics
How to do it...
How it works...
There's more...
See also
Formatting numbers
How to do it...
How it works...
There's more...
Converting between binary, octal, decimal, and hexadecimal
How to do it...
How it works...
Formatting with the correct plurals
Getting ready
How to do it...
How it works...
There's more...
Generating random numbers
How to do it...
How it works...
Operating complex numbers
How to do it...
How it works...
Converting between degrees and radians
How to do it...
How it works...
Taking logarithms
How to do it...
How it works...
Generating checksums
How to do it...
How it works...
Chapter 4: Once Upon a Time
Introduction
Finding today's date
How to do it...
How it works...
See also
Formatting date to string
How to do it...
How it works...
See also
Parsing the string into date
How to do it...
How it works...
Converting dates to epoch and vice versa
How to do it...
How it works...
Retrieving time units from the date
How to do it...
How it works...
Date arithmetics
How to do it...
How it works...
Finding the difference between two dates
How to do it...
How it works...
Converting between time zones
How to do it...
How it works...
Running the code block periodically
How to do it...
How it works...
Waiting a certain amount of time
How to do it...
How it works...
Timeout long-running operations
How to do it...
How it works...
There's more...
Serializing the time and date
How to do it...
How it works...
Chapter 5: In and Out
Introduction
Reading standard input
How to do it...
How it works...
Writing standard output and error
How to do it...
How it works...
Opening a file by name
How to do it...
How it works...
Reading the file into a string
How to do it...
How it works...
Reading/writing a different charset
How to do it...
How it works...
See also
Seeking a position within a file
How to do it...
How it works...
Reading and writing binary data
How to do it...
How it works...
Writing to multiple writers at once
How to do it...
How it works...
Piping between writer and reader
How to do it...
How it works...
Serializing objects to binary format
How to do it...
How it works...
Reading and writing ZIP files
How to do it...
How it works...
Parsing a large XML file effectively
How to do it...
How it works...
Extracting data from an incomplete JSON array
How to do it...
How it works...
Chapter 6: Discovering the Filesystem
Introduction
Getting file information
How to do it...
How it works...
Creating temporary files
How to do it...
How it works...
Writing the file
How to do it...
How it works...
Writing the file from multiple goroutines
How to do it...
How it works...
Listing a directory
How to do it...
How it works...
Changing file permissions
How to do it...
How it works...
Creating files and directories
How to do it...
How it works...
Filtering file listings
How to do it...
How it works...
See also
Comparing two files
How to do it...
How it works...
Resolving the user home directory
How to do it...
How it works...
Chapter 7: Connecting the Network
Introduction
Resolving local IP addresses
How to do it...
How it works...
Connecting to the remote server
How to do it...
How it works...
Resolving the domain by IP address and vice versa
How to do it...
How it works...
Connecting to the HTTP server
How to do it...
How it works...
See also
Parsing and building a URL
How to do it...
How it works...
Creating an HTTP request
How to do it...
How it works...
Reading and writing HTTP headers
How to do it...
How it works...
Handling HTTP redirects
How to do it...
How it works...
Consuming the RESTful API
How to do it...
How it works...
Sending a simple email
Getting ready
How to do it...
How it works...
Calling the JSON-RPC service
How to do it...
How it works...
Chapter 8: Working with Databases
Introduction
Connecting the database
Getting ready
How to do it...
How it works...
Validating the connection
Getting ready
How to do it...
How it works...
Executing statements
Getting ready
How to do it...
How it works...
Operations with prepared statements
Getting ready
How to do it...
How it works...
Canceling the pending query
Getting ready
How to do it...
How it works...
Reading query result metadata
Getting ready
How to do it...
How it works...
Retrieving data from a query result
Getting ready
How to do it...
How it works...
Parsing the query result into a map
Getting ready
How to do it...
How it works...
Handling transactions
Getting ready
How to do it...
How it works...
Executing stored procedures and functions
Getting ready
How to do it...
How it works...
Chapter 9: Come to the Server Side
Introduction
Creating the TCP server
How to do it...
How it works...
Creating the UDP server
How to do it...
How it works...
Handling multiple clients
How to do it...
How it works...
Creating the HTTP Server
How to do it...
How it works...
Handling HTTP requests
How to do it...
How it works...
Creating HTTP middleware layer
How to do it...
How it works...
Serving static files
How to do it...
How it works...
Serving content generated with templates
How to do it...
How it works...
Handling redirects
How to do it...
How it works...
Handling cookies
How to do it...
How it works...
Gracefully shutdown the HTTP server
How to do it...
How it works...
Serving secured HTTP content
Getting ready
How to do it...
How it works...
Resolving form variables
How to do it...
How it works...
Chapter 10: Fun with Concurrency
Introduction
Synchronizing access to a resource with Mutex
How to do it...
How it works...
Creating map for concurrent access
How to do it...
How it works...
Running a code block only once
How to do it...
How it works...
Pooling resources across multiple goroutines
How to do it...
How it works...
Synchronizing goroutines with WaitGroup
How to do it...
How it works...
Getting the fastest result from multiple sources
How to do it...
How it works...
Propagating errors with errgroup
How to do it...
How it works...
Chapter 11: Tips and Tricks
Introduction
Logging customization
How to do it...
How it works...
Testing the code
How to do it...
How it works...
See also
Benchmarking the code
How to do it...
How it works...
See also
Creating subtests
How to do it...
How it works...
See also
Testing the HTTP handler
How to do it...
How it works...
Accessing tags via reflection
How to do it...
How it works...
Sorting slices
How to do it...
How it works...
Breaking HTTP handlers into groups
How to do it...
How it works...
Utilizing HTTP/2 server push
Getting ready
How to do it...
How it works...
Other Books You May Enjoy
Index