Share E-Book

Go Standard Library Cookbook (Radomi_r Sohlich, [Radomír Sohlich etc.)(Z-Library)

Author Radomi­r Sohlich, [Radomír Sohlich, RadomГ­r Sohlich]

Go
Language English

No Description

Format EPUB
Size 6.6 MB
207
Views
0
Downloads
0.00
Total Donations

AI Guide

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

Full assistant
AI guide
# Go Standard Library Cookbook — Reading Guide ## 【One-Line Pitch】 A practical, recipe-driven tour of Go's standard library that shows you how to solve real-world problems—from string manipulation and date handling to file I/O and networking—without reaching for third-party packages. Ideal for Go developers who want to master the batteries-included tools the language ships with. ## 【Book Arc】 - **Opening (~0%–5%)**: Introduces the book's setup (Unix-based environment, Go 1.9.2+) and starts with system-level recipes—getting the Go runtime version, working with process IDs, and executing external commands via the `os/exec` package. - **Early (~5%–16%)**: Covers process management in depth (reading/writing child process I/O), then pivots to string manipulation—checking prefixes/suffixes, joining slices, indenting text, and using `text/tabwriter` for column-aligned output. - **Early (~16%–32%)**: Moves into data handling: regular expressions with `regexp`, CSV parsing, and numeric conversions (binary/octal/decimal/hex) plus floating-point comparison using the `big` package for arbitrary precision. Introduces random number generation with `math/rand` vs. `crypto/rand`. - **Middle (~32%–42%)**: Dedicated to time and date operations—formatting with Go's reference layout (Jan 2 15:04:05 2006 MST), parsing strings into dates, timezone conversions, duration calculations, and serializing time as RFC 3339 or epoch values. - **Middle (~42%–58%)**: Shifts to file and I/O operations—reading/writing standard output and error streams, file information retrieval, permission changes with `Chmod`, and directory traversal. Includes XML parsing with `encoding/xml` using streaming token-based decoding. - **Late (~58%–end)**: Covers networking fundamentals—resolving IP addresses and domain names, building and parsing URLs, creating HTTP requests, handling redirects, consuming RESTful APIs, and calling JSON-RPC services. ## 【Key Takeaways】 - **Process execution is straightforward with `os/exec`** (Early): Use `exec.Command` with either `Run()` for blocking execution or `Start()` + `Wait()` for async control; capture output via `bytes.Buffer` assigned to `prc.Stdout`. - **String operations have dedicated, efficient implementations** (Early): `strings.Contains` is built on `Index`, while `HasPrefix`/`HasSuffix` use length-checking comparisons—knowing these internals helps you choose the right tool for performance-sensitive code. - **Floating-point comparison requires tolerance or arbitrary precision** (Early): IEEE 754 representation causes precision loss (e.g., 1.3 becoming 1.29999999999); use the `big.Float` type with `SetPrec` for exact arithmetic when precision matters. - **Random number generation has two distinct tiers** (Early): `math/rand` is fast but cryptographically insecure (repeatable with a seed); switch to `crypto/rand` when security is a requirement—the sequences are non-repeatable. - **Go's time formatting uses a reference layout, not format codes** (Middle): The magic date "Jan 2 15:04:05 2006 MST" (numerically 1,2,3,4,5,6,-7) defines all layouts; the `time` package also provides predefined formats like `time.Kitchen`. - **File types implement both `Reader` and `Writer` interfaces** (Middle): This means any function accepting these interfaces works with files—`io.WriteString`, `fmt.Fprintln`, and custom readers all apply directly to `os.File`. - **XML parsing can be done incrementally with token decoding** (Middle): Using `xml.NewDecoder` with `decoder.Token()` lets you process large XML documents element-by-element without loading everything into memory, decoding matching elements into structs on the fly. - **URL construction and parsing are fully supported in the standard library** (Late): The `net/url` package lets you assemble URLs programmatically (scheme, host, path, query, user info) and parse them back into structured components. ## 【Reading Tips】 - **Skim the "How it works..." sections for architecture insights**: These explain not just what functions do but how they're implemented internally (e.g., `Contains` using `Index`), which helps you predict performance characteristics. - **Deep-read the process management and networking chapters**: These are the most complex and least intuitive parts of the standard library; the recipes on child process I/O and HTTP request handling are particularly valuable for real-world applications. - **Pay attention to the "There's more..." sections**: They often introduce important variations (like `FlagSet` for subcommand-style flag parsing) that the main recipe doesn't cover but you'll likely need. - **Treat the code samples as starting points, not final solutions**: The book uses simple examples (like `ls -a` for process execution) to demonstrate patterns you can adapt to your own use cases. - **Note the Unix assumption**: The book targets Unix-based systems; if you're on Windows, keep Cygwin or GitBash handy to follow along with the examples. ## 【Coverage Limits】 The excerpts cover roughly the first two-thirds of the book (through networking fundamentals). Later chapters on advanced networking, concurrency patterns, and testing are not represented in this guide. ##

Passage locations

Excerpt 1
commit, and the date or tag at the time of the binary build. The Version function, in fact, returns the runtime/internal/sys   .The Version   const...
View in text
Excerpt 2
plest ways to find the matching pattern in the given string. The only difference is that the FindString method of Regexp will return only the first occurrenc...
View in text
Excerpt 3
  in the Terminal. You will see the following output: Converting between binary, octal, decimal, and hexadecimal Converting between binary, octal, decim...
View in text
Excerpt 4
160; Reader interface are applicable to the  File type. The preceding example shows how to read the file with the use of Scanner and write the content t...
View in text

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List