出版社: O'Reilly Media, Inc.
副标题: An Idiomatic Approach to Real-World Go Programming
出版年: 2021-5-1
页数: 325
定价: USD 29.99
装帧: Paperback
ISBN: 9781492077145
内容简介 · · · · · ·
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...
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 explains the rationale behind the Go team’s design decisions and reveals design patterns that experienced Go developers have adopted. 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
Explore which libraries to select when solving problems
Learn how Go’s features allow the language to run efficiently
Know which Go features you should use sparingly, or not at all
作者简介 · · · · · ·
Distinguished Engineer at Capital One. Interested in programming languages, open source, technology trends, and the future of software engineering.
目录 · · · · · ·
Preface
Who Should Read This Book
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
· · · · · · (更多)
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
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
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
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
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
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
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
· · · · · · (收起)
喜欢读"Learning Go"的人也喜欢 · · · · · ·
Learning Go的书评 · · · · · · ( 全部 1 条 )
Go语言学习指南-书评
> 更多书评 1篇
论坛 · · · · · ·
在这本书的论坛里发言这本书的其他版本 · · · · · · ( 全部3 )
-
机械工业出版社 (2022)7.4分 35人读过
-
Oreilly & Associates Inc (2024)暂无评分 4人读过
以下书单推荐 · · · · · · ( 全部 )
- 书单|Oscar's tech selection (Oscar)
- Golang (fishball)
- 书单|Go Recipes (Happen)
- Go后端开发_Go语言 (SyStemOUt)
- 书单|Golang (polar9527)
谁读这本书? · · · · · ·
二手市场
· · · · · ·
- 在豆瓣转让 有33人想读,手里有一本闲着?
订阅关于Learning Go的评论:
feed: rss 2.0
1 有用 枝杈 2021-07-19 16:52:12
重温细节,适合入门用。
0 有用 光明 2024-03-03 17:37:05 浙江
我感觉这本书,我有点堵不下去了。 在重新读完中文版的基础上,算是作为补充又看了一次。说实话不错的一本书。 有广度,深度没有到让人看不明白的地方。
0 有用 闷 2022-02-13 22:46:17
入门书
0 有用 Geo 2023-12-28 21:57:52 广东
暂时用不上go,看一下了解一下go语言设计。挺好的一本go入门书,虽然可能略微有点啰嗦,但是对于入门者来说还是很详细的
0 有用 学fei了 2022-11-27 22:52:12 山西
不错的,适合已经掌握一门语言的人看,但也不是特别深入,国内好多书都是go的源码剖析,有些卷
0 有用 光明 2024-03-03 17:37:05 浙江
我感觉这本书,我有点堵不下去了。 在重新读完中文版的基础上,算是作为补充又看了一次。说实话不错的一本书。 有广度,深度没有到让人看不明白的地方。
0 有用 Geo 2023-12-28 21:57:52 广东
暂时用不上go,看一下了解一下go语言设计。挺好的一本go入门书,虽然可能略微有点啰嗦,但是对于入门者来说还是很详细的
0 有用 eleven 2023-07-10 10:52:20 广东
go语言特性确实精简,这本书也介绍的不错
0 有用 Asam 2023-06-24 08:49:50 湖北
语言精炼简洁,内容很新,适合入门,要是加一些练习题在里面就好了
0 有用 贫道爱妖孽 2023-02-25 15:25:00 北京
对于入门书来说,写的其实还可以。可能有些地方读起来有些啰嗦,但是还能读出些干货