Go Standard Library Cookbook

Book description

Implement solutions by leveraging the power of the GO standard library and reducing dependency on external crates

About This Book

  • Develop high quality, fast and portable applications by leveraging the power of Go Standard Library.
  • Practical recipes that will help you work with the standard library algorithms to boost your productivity as a Go developer.
  • Compose your own algorithms without forfeiting the simplicity and elegance of the Standard Library.

Who This Book Is For

This book is for Go developers who would like to explore the power of Golang and learn how to use the Go standard library for various functionalities. The book assumes basic Go programming knowledge.

What You Will Learn

  • Access environmental variables
  • Execute and work with child processes
  • Manipulate strings by performing operations such as search, concatenate, and so on
  • Parse and format the output of date/time information
  • Operate on complex numbers and effective conversions between different number formats and bases
  • Work with standard input and output
  • Handle filesystem operations and file permissions
  • Create TCP and HTTP servers, and access those servers with a client
  • Utilize synchronization primitives
  • Test your code

In Detail

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.

Style and approach

Solution based approach showcasing the power of Go standard library for easy practical implementations.

Table of contents

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

Product information

  • Title: Go Standard Library Cookbook
  • Author(s): Radomir Sohlich
  • Release date: February 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788475273