Categorygithub.com/efficientgo/examples
module
0.0.0-20240107094241-3b473a0075bd
Repository: https://github.com/efficientgo/examples.git
Documentation: pkg.go.dev

# README

"Efficient Go" Book Code Examples

Hi 👋

img.png

My name is Bartek Płotka and I wrote "Efficient Go". This book teaches pragmatic approaches to software efficiency and optimizations. While the majority of the learnings works for any programming language, it's best to learn on specific examples. For that purpose I teach how to make my favorite language more efficient - Go.

In this open-source repository you can find all examples from the book with tests, additional comments and more! Play with the examples to learn more about CPU, memory, OS and Go runtime characteristics I explained in my book!

NOTE: Don't import this module to your production code--it is meant for learning purposes only. Instead, we maintain production grade utilities mentioned in the book in core and e2e modules.

Index of Examples From Book

All examples from the book are buildable and tested in CI (as they should). See their location in the below table. For most of the code there exists corresponding _test.go file with tests and microbenchmarks when relevant.

For the book version before errata, see v1.0 branch.

NOTE: Some function names in examples might be different in book vs in code, due to name clashes. Code in the book might be also simplified. I also use non-conventional naming convention with some functions e.g. FailureRatio_Better. In Go all names should have camelCase form, so e.g. FailureRatioBetter. However, I chose to keep underscore to separate different versions of the same functions for book purposes - so the "production name" should is still FailureRatio! (:

Example RefPagePath to code/function in this repository.
Example 1-19pkg/getter/getter.go:13 FailureRatio
Example 1-210pkg/getter/getter.go:29 FailureRatio_Better
Example 1-312pkg/prealloc/slice.go:5 createSlice
Example 1-412pkg/prealloc/slice.go:14 createSlice_Better
Example 1-513pkg/notationhungarian/hung.go:5 structSystem
Example 2-137pkg/basic/basic.go:6
Example 2-242pkg/export/export.go:6
Example 2-344Prometheus main.go
Example 2-448pkg/errors/errors.go:8
Example 2-549pkg/errors/errors.go:34
Example 2-651pkg/basicserver/basicserver.go:10
Example 2-752pkg/unused/unused.go:8
Example 2-853-54pkg/testing/max_test.go:11
Example 2-955pkg/godoc
Example 2-1056pkg/godoc
Example 2-1159pkg/oop/oop.go:14
Example 2-1262sort.Interface from the standard library
Example 2-1362pkg/oop/oop.go:58
Example 2-1464pkg/generics/sort.go:12
Example 2-1565pkg/generics/blocks.go:19
Example 3-393pkg/jpeg/jpeg.go:12
Example 4-1115pkg/sum/sum.go:15 Sum
Example 4-5139pkg/concurrency/concurrency.go:12
Example 4-6140pkg/concurrency/concurrency.go:36
Example 4-7140pkg/concurrency/concurrency.go:55
Example 4-8140pkg/concurrency/concurrency.go:77
Example 5-1162pkg/memory/mmap/mmap.go:14
Example 5-2164pkg/memory/mmap/interactive/interactive_open.go:94
Example 5-3165pkg/memory/mmap/interactive/interactive_mmap.go:14
Example 5-4179pkg/memory/vars/vars.go:11
Example 5-5183pkg/memory/slice/slice.go:30
Example 5-6187pkg/memory/slice/slice.go:30
Example 6-1199pkg/metrics/latency_test.go:31
Example 6-2200-201pkg/metrics/latency_test.go:50
Example 6-3201pkg/metrics/latency_test.go:74
Example 6-4203pkg/metrics/latency_test.go:85
Example 6-6206pkg/metrics/latency_test.go:107
Example 6-7211-212pkg/metrics/latency_test.go:129
Example 6-9214pkg/metrics/prom.yaml
Example 6-11231pkg/metrics/cpu_test.go:19
Example 6-12235pkg/metrics/mem_test.go:69 printMemRuntimeMetric
Example 7-1241pkg/sum/sum.go:15 Sum
Example 8-1279pkg/sum/sum_test.go:49 BenchmarkSum
Example 8-2281pkg/sum/sum_test.go:49 BenchmarkSum
Example 8-9291pkg/sum/sum_test.go:76 TestSum
Example 8-10292pkg/sum/sum_test.go:49 BenchmarkSum
Example 8-11293pkg/sum/sum_test.go:116 TestBenchSum
Example 8-12295pkg/sum/sum_test.go:49 BenchmarkSum
Example 8-13296pkg/sum/sum_test.go:25 lazyCreateTestInput
Example 8-14297pkg/sum/sum_test.go:155 BenchmarkSum_AcrossInputs
Example 8-16302pkg/compileroptimizeaway/opt_away_test.go:11 BenchmarkPopcnt_Wrong
Example 8-18304-305pkg/compileroptimizeaway/opt_away_test.go:37 BenchmarkPopcnt_Sink
Example 8-19312-313pkg/sum/labeler/labeler_e2e_test.go:39 TestLabeler_LabelObject
Example 8-20313-314pkg/sum/labeler/labeler_e2e_test.go:39 TestLabeler_LabelObject
Example 9-1333pkg/profile/fd/fd.go
Example 9-2334-336pkg/profile/fd/example/main.go
Example 9-4344pkg/profile/fd/example/main.go:52
Example 9-5358pkg/profile/fd/http.go:12
Example 9-6374-375pkg/sum/labeler/labeler_e2e_test.go:39 TestLabeler_LabelObject
Example 10-2385pkg/sum/sum_test.go:142 BenchmarkSum_fgprof
Example 10-3388pkg/sum/sum.go:43 Sum2
Example 10-4390-391pkg/sum/sum.go:94 Sum3
Example 10-5393pkg/sum/sum.go:143 Sum4
Example 10-7396pkg/sum/sum.go:191 Sum5
Example 10-8398-399pkg/sum/sum.go:252 Sum6
Example 10-10403pkg/sum/sum_concurrent.go:18 ConcurrentSum1
Example 10-11405-406pkg/sum/sum_concurrent.go:49 ConcurrentSum2
Example 10-12407-408pkg/sum/sum_concurrent.go:109 ConcurrentSum3
Example 10-13410pkg/sum/sum_concurrent.go:181 ConcurrentSum4
Example 10-15412pkg/sum/sum.go:299 Sum7
Example 11-1418pkg/generics/dups.go:6
Example 11-2428-429pkg/leak/http_close.go:13
Example 11-3429-430pkg/leak/http_close_test.go:17 TestHandleCancel
Example 11-5431-432pkg/leak/http_close.go:39
Example 11-6433pkg/leak/http_close.go:87 Handle_Better
Example 11-7434pkg/leak/http_close_test.go:81
Example 11-8435-436pkg/leak/file.go:15
Example 11-9437pkg/leak/file.go:55
Example 11-10438-439pkg/leak/http_exhaust.go:13
Example 11-11441pkg/prealloc/prealloc_test.go:54
Example 11-12442-443pkg/prealloc/prealloc_test.go:99
Example 11-14444-445pkg/prealloc/linkedlist.go:6
Example 11-15446pkg/prealloc/linkedlist.go:39
Example 11-16448pkg/prealloc/linkedlist.go:57
Examples 11-17450pkg/pools/reuse.go:8
Examples 11-18451-452pkg/pools/reuse.go:8
Examples 11-19452-453pkg/pools/reuse.go:8
Examples 11-20453pkg/pools/reuse_test.go:39

Copyright Bartek Płotka

# Packages

No description provided by the author