Skip to content

inancgumus/gobyexample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go by Example: Programmer's Guide to Idiomatic and Testable Code

Unlock Go's unique perspective on program design and start writing simple, maintainable, efficient, and testable code through hands-on, realistic project examples.

You can read the book at https://mng.bz/DwMy.

Read Go by Example

What is this book about?

  • Example projects based on real-world scenarios.
  • Writing idiomatic, efficient, and testable Go code from scratch.
  • Writing code that is easy to read, maintain, and extend.
  • Demonstrating Go's unique features and using them as they were intended.
  • Go patterns, best practices, and common pitfalls to avoid.

Who is this book for?

  • Expert programmers proficient in another Object-Oriented language and want to learn idiomatic Go.
  • Gophers who want to improve their skills and knowledge for writing idiomatic, efficient, and testable code.

This book includes a Go crash course in Appendices.

Table of Contents

Tip

Click on the chapter headings to see the code listings.

This chapter explores the book's goals, introduces Go, and showcases Go's prominent features without fully delving into Go's mechanics and idioms. It gets you started with Go and prepares you for the more advanced topics covered in the book.

Topics covered

  • Why should you read this book?
  • Importance of writing idiomatic and testable code
  • Presenting Go's prominent features

This chapter focuses on writing idiomatic code and testing.

Topics covered

  • Exploring idiomatic guidelines with packages
  • Satisfying the standard library interfaces
  • Writing and running tests using the tools provided by Go
  • Using table-driven testing and subtests to improve maintainability
  • Writing example tests to generate runnable documentation

This chapter covers the importance of test coverage and performance optimization.

Topics covered

  • Measuring test coverage to see what percentage of code is tested
  • Optimizing for performance using benchmarks and profiling
  • Parallel testing to reduce test run time and detect data race issues

This chapter uses the standard library alone to write idiomatic command-line programs.

Topics covered

  • Structuring and writing user-friendly command-line tools
  • Parsing command-line arguments and flags
  • Exploring the standard library's os and flag packages
  • Extending the flag package with custom types

This chapter explores how to decouple code from its dependencies to improve testability and maintainability.

Topics covered

  • Exploring challenges to testability
  • Improving testability by isolating dependencies
  • Using dependency injection techniques for testability

This chapter focuses on designing and structuring synchronous APIs that treat concurrency as an implementation detail.

Topics covered

  • Designing synchronous APIs that treat concurrency as an implementation detail
  • Standardizing the processing of sequences of values using push iterators
  • Structuring concurrent processing with the pipeline pattern

This chapter focuses on writing responsive and efficient programs, using HTTP as a case study.

Topics covered

  • Propagating cancellation signals using the context package
  • Efficiently processing HTTP using the net/http package
  • Efficiently consuming byte streams using the io package
  • Interface embedding and composition mechanics
  • Idiomatic testing techniques using the RoundTripper interface
  • Testing against a test HTTP server using the httptest package

This chapter focuses on package organization and structuring that avoids import cycles, using HTTP services as a case study.

Topics covered

  • Effectively organizing and structuring packages to avoid import cycles
  • Simplifying the use of types by making their zero value useful
  • Protecting against unsafe concurrent use of shared state with mutexes
  • Implementing and testing HTTP servers with the http and httptest packages
  • Recording HTTP responses with custom test helpers to streamline testing

This chapter focuses on composition patterns to demonstrate how to design packages that offer reusable and composable functionality.

Topics covered

  • Effectively using composition patterns and functional programming techniques
  • Leveraging field embedding and method forwarding to reuse functionality
  • Understanding optional interfaces, method hiding issues, and type assertions
  • Propagating request-scoped values across package APIs Using Context
  • Implementing custom slog.Handlers to log extra attributes automatically
  • Wrapping interfaces to modify, extend, and intercept existing behavior
  • Extracting implicit behavior using anonymous interfaces

This chapter explains using implicit interfaces to decouple storage from its consumers and highlights consumer-defined interfaces.

Topics covered

  • Interacting with SQL databases
  • Understanding the driver pattern
  • Integration-testing external services
  • Leveraging consumer-driven interface approach

This appendix introduces the concept of modules and packages.

This appendix covers the basics of variables and pointers.

This appendix covers the basics of arrays, slices, and maps.

This appendix covers the basics of object-oriented programming in Go.

This appendix covers the basics of concurrent programming in Go.

Topics covered

  • Goroutines and their usage
  • Using WaitGroup to synchronize goroutines
  • Unbuffered channels for communication between goroutines
  • Closing channels to signal completion
  • Using select to handle multiple channel operations
  • Buffered channels for decoupling goroutines
  • Many other widely used concurrency patterns

This appendix covers Rob Pike's self-referential options pattern and compares it with other approaches.

This appendix shows how to cross-compile Go programs for different operating systems and architectures.