Go Cookbook

Book description

Go is an increasingly popular language for programming everything from web applications to distributed network services. This practical guide provides recipes to help you unravel common problems and perform useful tasks when working with Go. Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work. Programmers new to Go can quickly ramp up their knowledge while accomplishing useful tasks, and experienced Go developers can save time by cutting and pasting proven code directly into their applications.

Recipes include:

  • Creating a module
  • Calling code from another module
  • Returning and handling an error
  • Converting strings to numbers (or converting numbers to strings)
  • Modifying multiple characters in a string
  • Creating substrings from a string
  • Capturing string input
  • And so much more

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
  2. 1. Getting Started Recipes
    1. 1.0. Introduction
    2. 1.1. Installing Go
    3. 1.2. Playing Around with Go
    4. 1.3. Writing a Hello World Program
    5. 1.4. Using an External Package
    6. 1.5. Handling Errors
    7. 1.6. Logging Events
    8. 1.7. Testing Your Code
  3. 2. Module Recipes
    1. 2.0. Introduction
    2. 2.1. Creating a Go Module
    3. 2.2. Importing Dependent Packages Into Your Module
    4. 2.3. Removing Dependent Packages from Your Module
    5. 2.4. Find Available Versions of Third-Party Packages
    6. 2.5. Importing a Specific Version of a Dependent Package Into Your Module
    7. 2.6. Requiring Local Versions of Dependent Packages
    8. 2.7. Using Multiple Versions of the Same Dependent Packages
  4. 3. Error Handling Recipes
    1. 3.0. Introduction
    2. 3.1. Handling Errors
    3. 3.2. Simplifying Repetitive Error Handling
    4. 3.3. Creating Customized Errors
    5. 3.4. Wrapping an Error with Other Errors
    6. 3.5. Inspecting Errors
    7. 3.6. Handling Errors with Panic
    8. 3.7. Recovering from Panic
    9. 3.8. Handling Interrupts
  5. 4. Logging Recipes
    1. 4.0. Introduction
    2. 4.1. Writing to Logs
    3. 4.2. Change What Is Being Logged by the Standard Logger
    4. 4.3. Logging to File
    5. 4.4. Using Log Levels
    6. 4.5. Logging to the System Log Service
  6. 5. Function Recipes
    1. 5.0. Introduction
    2. 5.1. Defining a Function
    3. 5.2. Accepting Multiple Data Types with a Function
    4. 5.3. Accepting a Variable Number of Parameters
    5. 5.4. Accepting Parameters of Any Type
    6. 5.5. Creating an Anonymous Function
    7. 5.6. Creating a Function That Maintains State After It Is Called
  7. 6. String Recipes
    1. 6.0. Introduction
    2. 6.1. Creating Strings
    3. 6.2. Converting String to Bytes and Bytes to String
    4. 6.3. Creating Strings from Other Strings and Data
    5. 6.4. Converting Strings to Numbers
    6. 6.5. Converting Numbers to Strings
    7. 6.6. Replacing Multiple Characters in a String
    8. 6.7. Creating a Substring from a String
    9. 6.8. Checking if a String Contains Another String
    10. 6.9. Splitting a String Into an Array of Strings or Combining an Array of Strings Into a String
    11. 6.10. Trimming Strings
    12. 6.11. Capturing String Input from the Command Line
    13. 6.12. Escaping and Unescaping HTML Strings
    14. 6.13. Using Regular Expressions
  8. 7. General Input/Output Recipes
    1. 7.0. Introduction
    2. 7.1. Reading from an Input
    3. 7.2. Writing to an Output
    4. 7.3. Copying from a Reader to a Writer
    5. 7.4. Reading from a Text File
    6. 7.5. Writing to a Text File
    7. 7.6. Using a Temporary File
  9. 8. CSV Recipes
    1. 8.0. Introduction
    2. 8.1. Reading the Whole CSV File
    3. 8.2. Reading a CSV File One Row at a Time
    4. 8.3. Unmarshalling CSV Data Into Structs
    5. 8.4. Removing the Header Line
    6. 8.5. Using Different Delimiters
    7. 8.6. Ignoring Rows
    8. 8.7. Writing CSV Files
    9. 8.8. Writing to File One Row at a Time
  10. 9. JSON Recipes
    1. 9.0. Introduction
    2. 9.1. Parsing JSON Data Byte Arrays to Structs
    3. 9.2. Parsing Unstructured JSON Data
    4. 9.3. Parsing JSON Data Streams Into Structs
    5. 9.4. Creating JSON Data Byte Arrays from Structs
    6. 9.5. Creating JSON Data Streams from Structs
    7. 9.6. Omitting Fields in Structs
  11. 10. Binary Recipes
    1. 10.0. Introduction
    2. 10.1. Encoding Data to gob Format Data
    3. 10.2. Decoding gob Format Data to Structs
    4. 10.3. Encoding Data to a Customized Binary Format
    5. 10.4. Decoding Data with a Customized Binary Format to Structs
  12. 11. Date and Time Recipes
    1. 11.0. Introduction
    2. 11.1. Telling Time
    3. 11.2. Doing Arithmetic with Time
    4. 11.3. Representing Dates
    5. 11.4. Representing Time Zones
    6. 11.5. Representing Duration
    7. 11.6. Pausing for a Specific Duration
    8. 11.7. Measuring Lapsed Time
    9. 11.8. Formatting Time for Display
    10. 11.9. Parsing Time Displays Into Structs
  13. 12. Structs Recipes
    1. 12.0. Introduction
    2. 12.1. Defining Structs
    3. 12.2. Creating Struct Methods
    4. 12.3. Creating and Using Interfaces
    5. 12.4. Creating Struct Instances
    6. 12.5. Creating One-Time Structs
    7. 12.6. Composing Structs from Other Structs
    8. 12.7. Defining Metadata for Struct Fields
  14. 13. Data Structure Recipes
    1. 13.0. Introduction
    2. 13.1. Creating Arrays or Slices
    3. 13.2. Accessing Arrays or Slices
    4. 13.3. Modifying Arrays or Slices
    5. 13.4. Making Arrays and Slices Safe for Concurrent Use
    6. 13.5. Sorting Arrays of Slices
    7. 13.6. Creating Maps
    8. 13.7. Accessing Maps
    9. 13.8. Modifying Maps
    10. 13.9. Sorting Maps
  15. 14. More Data Structure Recipes
    1. 14.0. Introduction
    2. 14.1. Creating Queues
    3. 14.2. Creating Stacks
    4. 14.3. Creating Sets
    5. 14.4. Creating Linked Lists
    6. 14.5. Creating Heaps
    7. 14.6. Creating Graphs
    8. 14.7. Finding the Shortest Path on a Graph
  16. 15. Image-Processing Recipes
    1. 15.0. Introduction
    2. 15.1. Loading an Image from a File
    3. 15.2. Saving an Image to a File
    4. 15.3. Creating Images
    5. 15.4. Flipping an Image Upside Down
    6. 15.5. Converting an Image to Grayscale
    7. 15.6. Resizing an Image
  17. 16. Networking Recipes
    1. 16.0. Introduction
    2. 16.1. Creating a TCP Server
    3. 16.2. Creating a TCP Client
    4. 16.3. Creating a UDP Server
    5. 16.4. Creating a UDP Client
  18. 17. Web Recipes
    1. 17.0. Introduction
    2. 17.1. Creating a Simple Web Application
    3. 17.2. Handling HTTP Requests
    4. 17.3. Handling HTML Forms
    5. 17.4. Uploading a File to a Web Application
    6. 17.5. Serving Static Files
    7. 17.6. Creating a JSON Web Service API
    8. 17.7. Serving Through HTTPS
    9. 17.8. Using Templates for Go Web Applications
    10. 17.9. Making an HTTP Client Request
  19. 18. Testing Recipes
    1. 18.0. Introduction
    2. 18.1. Automating Functional Tests
    3. 18.2. Running Multiple Test Cases
    4. 18.3. Setting Up and Tearing Down Before and After Tests
    5. 18.4. Creating Subtests to Have Finer Control Over Groups of Test Cases
    6. 18.5. Running Tests in Parallel
    7. 18.6. Generating Random Test Inputs for Tests
    8. 18.7. Measuring Test Coverage
    9. 18.8. Testing a Web Application or a Web Service
  20. 19. Benchmarking Recipes
    1. 19.0. Introduction
    2. 19.1. Automating Performance Tests
    3. 19.2. Running Only Performance Tests
    4. 19.3. Avoiding Test Fixtures in Performance Tests
    5. 19.4. Changing the Timing for Running Performance Tests
    6. 19.5. Running Multiple Performance Test Cases
    7. 19.6. Comparing Performance Test Results
    8. 19.7. Profiling a Program
  21. Index
  22. About the Author

Product information

  • Title: Go Cookbook
  • Author(s): Sau Sheong Chang
  • Release date: September 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098122119