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
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# Go Cookbook: Expert Solutions for Commonly Needed Go Tasks
## 【One-Line Pitch】
A practical, recipe-driven guide for Go developers who want quick, proven solutions to everyday programming challenges—from module management and error handling to string manipulation and file I/O. Perfect for both newcomers looking to build confidence and experienced developers seeking copy-paste-ready code.
## 【Book Arc】
- **Opening (~0%–10%)**: Covers installation, the Go Playground, writing your first Hello World program, using external packages, basic error handling, logging, and testing with Go's built-in testing tool. Establishes the fundamental workflow for all subsequent recipes.
- **Early (~10%–23%)**: Dives into Go modules—creating them, importing and removing dependencies, managing versions, and working with local packages. Also introduces error handling philosophy, contrasting Go's error-return approach with exception-based languages like Python and Java.
- **Early (~23%–32%)**: Explores advanced error handling patterns including custom error types, the error interface, panic and recover mechanisms, plus an introduction to logging with syslog standards and generics in function definitions.
- **Middle (~32%–48%)**: Focuses on functions—anonymous functions, type definitions, and generics with type constraints—then transitions to string manipulation recipes covering number-to-string and string-to-number conversions, substring operations, and input capture techniques.
- **Middle (~48%–52%+)**: Covers general I/O operations including reading from readers, working with file statistics, reading files into byte arrays, and writing to text files. Emphasizes the reader/writer pattern that underpins Go's I/O design.
## 【Key Takeaways】
- **Go modules are the backbone of dependency management** (Early): The `go.mod` file tracks all dependencies, and `go get` seamlessly integrates third-party packages into your workflow without disrupting existing habits. Understanding module creation and version management is essential for any real-world Go project.
- **Go treats errors as values, not exceptions** (Early): Unlike Python or Java, Go doesn't use try/catch—instead, functions return errors that callers must handle explicitly. This unconventional approach, while requiring more verbose code, makes error paths visible and forces deliberate handling.
- **Custom errors are just structs with an Error() method** (Early): Implementing the `error` interface is straightforward—any struct with an `Error() string` method qualifies. You can add fields to carry rich context, like line and column numbers for syntax errors.
- **Panic and recover provide controlled crash handling** (Early): Panic stops normal execution but runs deferred functions before bubbling up; recover, which only works inside deferred functions, can stop a panic and let your program continue running.
- **Generics enable type-safe, reusable functions** (Early): Using type parameters with constraints like `int | float64`, you can write a single `Add` function that works across numeric types. The tilde operator (`~`) extends constraints to custom types with matching underlying types.
- **String conversions are one-way streets** (Middle): Converting numbers to strings with `strconv.Itoa` or `FormatInt` never returns errors—it's always possible—while parsing strings to numbers can fail. Format functions also support bases from 2 to 36, enabling binary conversions.
- **The strings package offers multiple substring search tools** (Middle): `Contains`, `Index`, `Count`, `HasPrefix`, and `HasSuffix` provide different approaches, with `Contains` being a convenience wrapper around `Index` with identical performance.
- **Readers and writers are the universal I/O abstraction** (Middle): Functions expecting readers can accept anything from files to strings via `strings.NewReader`, and `io.ReadAll` provides an intuitive way to consume data from any reader source.
## 【Reading Tips】
- **Skim the first chapter** if you're already comfortable with Go basics—the installation and Hello World content is standard, but the testing recipe (using `_test.go` files) is worth a quick look.
- **Deep-read the error handling chapter** (Chapter 3)—it's where Go diverges most from other languages, and understanding the error-as-value philosophy will shape how you write all your Go code.
- **Pay special attention to the generics discussion** in the functions chapter—it's a newer Go feature that can dramatically reduce code duplication, but the type constraint syntax takes practice.
- **Use the string manipulation recipes as a reference** rather than reading them sequentially—they're perfect for looking up specific functions when you need them.
- **The I/O chapter rewards hands-on experimentation**—try modifying the file reading examples to understand how the reader pattern works before applying it to network streams or other sources.
## 【Coverage Limits】
The excerpts primarily cover the first half of the book (through approximately 52%), focusing on modules, errors, functions, strings, and basic I/O. Later chapters on web applications, concurrency, and distributed services are not covered in this guide.
##
Excerpt 1
4 1.4 Using an External Package 5 1.5 Handling Errors ...
# github.com/gorilla/mux v1.8.0 ## explicit; go 1.12 github.com/gorilla/mux By default, Go will use the version in the vendor directory if your Go version is...
er of type constraints, for example, the Signed constraint: type Signed interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 } The tilde (~) operator indi...
he ReadString function on a Reader wrapped around os.Stdin: func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Please enter many words: ") input, e...
from the tz database that’s in the computer it’s running on. If you want to use different data, you can use the LoadLocationFromTZ Data function instead. 154...
ent. The value is still returned but since you know the key doesn’t exist and it’s just a zero-value, you probably would not use it. You can also use a for ....
emove the edge from the other side for n := range g.Edges { removeEdge(g, n, name) } } You also need to remove the edges that are connected to the node. Firs...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Go Cookbook Expert Solutions for Commonly Needed Go Tasks (Sau Sheong Chang) (Z-Library) (1)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Go Cookbook Expert Solutions for Commonly Needed Go Tasks (Sau Sheong Chang) (Z-Library) (1)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment