Learning Go: An Idiomatic Approach to Real-World Go Programming
- Length: 360 pages
- Edition: 1
- Language: English
- Publisher: O'Reilly Media
- Publication Date: 2021-03-30
- ISBN-10: 1492077216
- ISBN-13: 9781492077213
- Sales Rank: #843481 (See Top 100 Books)
Go is rapidly becoming the preferred language for building web services. There are plenty of tutorials available that teach Go’s syntax to developers with experience in other programming languages. But tutorials aren’t enough. They don’t teach Go’s idioms, so developers end up recreating patterns that don’t make sense in a Go context. This practical guide provides the essential background you need to write clear and idiomatic Go.
No matter your level of experience, you’ll learn how to think like a Go developer. Author Jon Bodner reveals design patterns that experienced Go developers have adopted and the rationale for them. You’ll learn how to structure your project and choose the proper tools and libraries to create successful software.
- Learn how to write idiomatic code in Go and design a Go project
- Understand the reasons for the design decisions in Go
- Set up a Go development environment for a solo developer or team
- Learn how and when to use reflection, unsafe, and CGo
- Learn how Go’s features allow the language to run efficiently
- Know which Go features you should use sparingly, or not at all
- Learn the future of Go, including Generics
Preface Who Should Read This Book Conventions Used in This Book Using Code Examples O’Reilly Online Learning How to Contact Us Acknowledgments 1. Setting Up Your Go Environment Installing the Go Tools The Go Workspace The go Command go run and go build go run go build Getting Third-Party Go Tools Formatting Your Code Linting and Vetting Choose Your Tools Visual Studio Code GoLand The Go Playground Makefiles Staying Up to Date Wrapping Up 2. Primitive Types and Declarations Built-in Types The Zero Value Literals Booleans Numeric Types Integer types The special integer types Choosing which integer to use Integer operators Floating point types Complex types (you’re probably not going to use these) A Taste of Strings and Runes Explicit Type Conversion var Versus := Using const Typed and Untyped Constants Unused Variables Naming Variables and Constants Wrapping Up 3. Composite Types Arrays—Too Rigid to Use Directly Slices len append Capacity make Declaring Your Slice Slicing Slices Slices share storage sometimes Converting Arrays to Slices copy Strings and Runes and Bytes Maps Reading and Writing a Map The comma ok Idiom Deleting from Maps Using Maps as Sets Structs Anonymous Structs Comparing and Converting Structs Wrapping Up 4. Blocks, Shadows, and Control Structures Blocks Shadowing Variables Detecting Shadowed Variables if for, Four Ways The Complete for Statement The Condition-Only for Statement The Infinite for Statement break and continue The for-range Statement Iterating over maps Iterating over strings The for-range value is a copy Labeling Your for Statements Choosing the Right for Statement switch Blank Switches Choosing Between if and switch goto—Yes, goto Wrapping Up 5. Functions Declaring and Calling Functions Simulating Named and Optional Parameters Variadic Input Parameters and Slices Multiple Return Values Multiple Return Values Are Multiple Values Ignoring Returned Values Named Return Values Blank Returns—Never Use These! Functions Are Values Function Type Declarations Anonymous Functions Closures Passing Functions as Parameters Returning Functions from Functions defer Go Is Call By Value Wrapping Up 6. Pointers A Quick Pointer Primer Don’t Fear the Pointers Pointers Indicate Mutable Parameters Pointers Are a Last Resort Pointer Passing Performance The Zero Value Versus No Value The Difference Between Maps and Slices Slices as Buffers Reducing the Garbage Collector’s Workload Wrapping Up 7. Types, Methods, and Interfaces Types in Go Methods Pointer Receivers and Value Receivers Code Your Methods for nil Instances Methods Are Functions Too Functions Versus Methods Type Declarations Aren’t Inheritance Types Are Executable Documentation iota Is for Enumerations—Sometimes Use Embedding for Composition Embedding Is Not Inheritance A Quick Lesson on Interfaces Interfaces Are Type-Safe Duck Typing Embedding and Interfaces Accept Interfaces, Return Structs Interfaces and nil The Empty Interface Says Nothing Type Assertions and Type Switches Use Type Assertions and Type Switches Sparingly Function Types Are a Bridge to Interfaces Implicit Interfaces Make Dependency Injection Easier Wire Go Isn’t Particularly Object-Oriented (and That’s Great) Wrapping Up 8. Errors How to Handle Errors: The Basics Use Strings for Simple Errors Sentinel Errors Errors Are Values Wrapping Errors Is and As Wrapping Errors with defer panic and recover Getting a Stack Trace from an Error Wrapping Up 9. Modules, Packages, and Imports Repositories, Modules, and Packages go.mod Building Packages Imports and Exports Creating and Accessing a Package Naming Packages How to Organize Your Module Overriding a Package’s Name Package Comments and godoc The internal Package The init Function: Avoid if Possible Circular Dependencies Gracefully Renaming and Reorganizing Your API Working with Modules Importing Third-Party Code Working with Versions Minimum Version Selection Updating to Compatible Versions Updating to Incompatible Versions Vendoring pkg.go.dev Additional Information Publishing Your Module Versioning Your Module Module Proxy Servers Specifying a Proxy Server Private Repositories Wrapping Up 10. Concurrency in Go When to Use Concurrency Goroutines Channels Reading, Writing, and Buffering for-range and Channels Closing a Channel How Channels Behave select Concurrency Practices and Patterns Keep Your APIs Concurrency-Free Goroutines, for Loops, and Varying Variables Always Clean Up Your Goroutines The Done Channel Pattern Using a Cancel Function to Terminate a Goroutine When to Use Buffered and Unbuffered Channels Backpressure Turning Off a case in a select How to Time Out Code Using WaitGroups Running Code Exactly Once Putting Our Concurrent Tools Together When to Use Mutexes Instead of Channels Atomics—You Probably Don’t Need These Where to Learn More About Concurrency Wrapping Up 11. The Standard Library io and Friends time Monotonic Time Timers and Timeouts encoding/json Use Struct Tags to Add Metadata Unmarshaling and Marshaling JSON, Readers, and Writers Encoding and Decoding JSON Streams Custom JSON Parsing net/http The Client The Server Middleware Use idiomatic third-party modules to enhance the server Wrapping Up 12. The Context What Is the Context? Cancellation Timers Handling Context Cancellation in Your Own Code Values Wrapping Up 13. Writing Tests The Basics of Testing Reporting Test Failures Setting Up and Tearing Down Storing Sample Test Data Caching Test Results Testing Your Public API Use go-cmp to Compare Test Results Table Tests Checking Your Code Coverage Benchmarks Stubs in Go httptest Integration Tests and Build Tags Finding Concurrency Problems with the Race Checker Wrapping Up 14. Here There Be Dragons: Reflect, Unsafe, and Cgo Reflection Lets Us Work with Types at Runtime Types, Kinds, and Values Types and kinds Values Making New Values Use Reflection to Check If an Interface’s Value Is nil Use Reflection to Write a Data Marshaler Build Functions with Reflection to Automate Repetitive Tasks You Can Build Structs with Reflection, but Don’t Reflection Can’t Make Methods Only Use Reflection If It’s Worthwhile unsafe Is Unsafe Use unsafe to Convert External Binary Data unsafe Strings and Slices unsafe Tools Cgo Is for Integration, Not Performance Wrapping Up 15. A Look at the Future: Generics in Go Generics Reduce Repetitive Code and Increase Type Safety Introducing Generics in Go Use Type Lists to Specify Operators Generic Functions Abstract Algorithms Type Lists Limit Constants and Implementations Things That Are Left Out Idiomatic Go and Generics Further Futures Unlocked Wrapping Up Index
Donate to keep this site alive
How to download source code?
1. Go to: https://www.oreilly.com/
2. Search the book title: Learning Go: An Idiomatic Approach to Real-World Go Programming
, sometime you may not get the results, please search the main title
3. Click the book title in the search results
3. Publisher resources
section, click Download Example Code
.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.