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