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 to WeChat Moments
Go Cookbook Expert Solutions for Commonly Needed Go Tasks (Sau Sheong Chang)(Z-Library) (1)
Scan the QR code with WeChat to open this book
Tap the ··· menu in the top-right corner
Choose Share to Moments
You are already in WeChat. Tap ··· in the top-right corner and choose Share to Moments.
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
这本书是 Sau Sheong Chang 写的一本 Go 语言实用手册,面向的是那些已经对 Go 有基本了解、希望在日常开发中快速找到可靠做法的程序员。全书以“配方”的形式组织,每一个配方都围绕一个常见的具体任务展开,给出可以独立使用的完整代码,并解释这段代码为什么这样写、在什么情况下适用。它不追求把语言特性从头讲一遍,而是把重点放在实际工作中反复遇到的问题上,让读者可以按需查阅、直接拿来用。
从给出的材料看,读者可以抓住几条比较清晰的线索。第一条是工程实践的基础环节。书中涉及如何使用外部包、如何处理错误、如何记录日志事件以及如何测试代码,这些内容构成了一个 Go 项目从引入依赖到保证质量的基本流程。错误处理在 Go 中被认真对待,标准库的 errors 包提供了许多操作错误的函数,书中对此有专门的讨论。
第二条是数据处理与类型转换。材料提到 strconv 包中的 Parse 系列函数,并进一步讲解 Format 系列函数如何把数字转成字符串,指出格式化是解析的逆过程。这类配方适合需要频繁在字符串和数值之间转换的场景。
第三条是语言层面的抽象设计。
Digital LibraryAI Assistant
Hi — I can help you pick a book. Tell me a topic, language, or level, e.g. “a light English read”. If that's too broad, I'll ask one follow-up.
Loading comments...
Reply to Comment
Edit Comment