Skip to content
Learn Go with test-driven development
Go Other
  1. Go 99.1%
  2. Other 0.9%
Branch: master
Clone or download

Latest commit

ntguilty replacement of NewReader with NewScanner in NewCLI (#318)
changes in time.md due to probable transcription errors
Latest commit cebd502 Feb 13, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
arrays lint issues fixed and check is wired up to build #37 Apr 10, 2018
command-line replace _all_ of the with %q Jul 11, 2019
concurrency fixes #79 Jun 21, 2018
context follow consistency of separated test file Apr 26, 2019
di replace _all_ of the with %q Jul 11, 2019
for replace _all_ of the with %q Jul 11, 2019
hello-world Use %q rather than '%s' Jul 11, 2019
http-server fix the wrong use of RWMutex in http-server/v5 (#310) Jan 19, 2020
integers Update integers.md (#282) Nov 3, 2019
io cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
json replace _all_ of the with %q Jul 11, 2019
maps replace _all_ of the with %q Jul 11, 2019
math Fix test error message (#265) Oct 13, 2019
mocking Minor comment fixes (#295) Nov 23, 2019
pointers cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
q-and-a introduce errors.As in the error chapter to fix #277 Jan 30, 2020
reflection cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
roman-numerals cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
select select: change to empty struct and close (#276) Oct 25, 2019
structs Float string format to precision decimal #237 (#243) Sep 10, 2019
sync updates on community convention around mutex naming and position plus… Feb 7, 2019
time cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
todo more work on websockets and start of todo thing Oct 21, 2018
websockets update refactor htmlTemplatePath (#317) Feb 13, 2020
.editorconfig improve editorconfig Jun 18, 2018
.gitignore wip - cl - teasing towards using bufio.NewReader Jul 6, 2018
.mdlrc not allow MD010 hard tabs in Markdown files Jun 18, 2018
.travis.yml Upgrade golang to v1.13.1 (#258) Oct 5, 2019
LICENSE.md Update LICENSE.md Jan 6, 2020
README.md Add link to Travis Status SVG (#289) Nov 3, 2019
SUMMARY.md Add maths chapter to listings Jul 14, 2019
app-intro.md time is done Aug 27, 2018
arrays-and-slices.md Fix possible little inconsistency: want != got -> got != want (#249) Oct 1, 2019
book.json remove trailing space Apr 29, 2018
build.books.sh disable PDF generation for now Jul 15, 2019
build.sh make the sad spelling time stop Feb 1, 2019
command-line.md replace _all_ of the with %q Jul 11, 2019
concurrency.md Fix typo in concurrency.md (#311) Jan 28, 2020
context.md Add links to code to some chapters (#294) Nov 22, 2019
contributing.md add notes on contributing Jan 30, 2020
dependency-injection.md replace _all_ of the with %q Jul 11, 2019
epub-cover.png [ImgBot] Optimize images (#226) Aug 23, 2019
epub-cover.pxm changed my mind, added the words back to cover Feb 7, 2019
error-types.md introduce errors.As in the error chapter to fix #277 Jan 30, 2020
gb-readme.md add portuguese link (#195) Jul 2, 2019
go.mod add notes on contributing Jan 30, 2020
go.sum add notes on contributing Jan 30, 2020
hello-world.md Add godoc note for 1.13 in hello world chapter (#254) Oct 4, 2019
http-server.md Fix function name in chapter HTTP Server (#312) Feb 2, 2020
install-go.md fix formatting on modulepath to render it visible (#314) Feb 7, 2020
integers.md Update integers.md (#282) Nov 3, 2019
io.md Punctuation tweaks (#275) Nov 11, 2019
iteration.md Fix typos (#251) Oct 2, 2019
json.md replace _all_ of the with %q Jul 11, 2019
maps.md fixes #308 Jan 30, 2020
math.md SVG struct Width and Height (#263) Jan 30, 2020
meta.tex Add maths chapter to listings Jul 14, 2019
mocking.md Fix typo (#288) Nov 3, 2019
os-exec.md cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019
pdf-cover.md some improvements for PDF Feb 14, 2019
pointers-and-errors.md fixes #301 Jan 30, 2020
red-green-blue-gophers-smaller.png [ImgBot] Optimize images (#226) Aug 23, 2019
red-green-blue-gophers.png [ImgBot] Optimize images (#226) Aug 23, 2019
reflection.md Minor comment fixes (#295) Nov 23, 2019
roman-numerals.md fixing some inconsistencies that brought about some confusion for #298 Jan 30, 2020
select.md select: change to empty struct and close (#276) Oct 25, 2019
structs-methods-and-interfaces.md Fixed typo (#304) Jan 2, 2020
sync.md Fix formatting issue on sync.md (#303) Dec 21, 2019
template.md fix blank line Apr 29, 2018
time.md replacement of NewReader with NewScanner in NewCLI (#318) Feb 13, 2020
title.txt some corrections to websockets and an attempt at a cover image for th… Feb 1, 2019
websockets.md update refactor htmlTemplatePath (#317) Feb 13, 2020
why.md cant, dont, wont, didnt -> contraction fix (#233) Sep 9, 2019

README.md

Learn Go with Tests

Art by Denise

Build Status Go Report Card

Why

Table of contents

Go fundamentals

  1. Install Go - Set up environment for productivity.
  2. Hello, world - Declaring variables, constants, if/else statements, switch, write your first go program and write your first test. Sub-test syntax and closures.
  3. Integers - Further Explore function declaration syntax and learn new ways to improve the documentation of your code.
  4. Iteration - Learn about for and benchmarking.
  5. Arrays and slices - Learn about arrays, slices, len, varargs, range and test coverage.
  6. Structs, methods & interfaces - Learn about struct, methods, interface and table driven tests.
  7. Pointers & errors - Learn about pointers and errors.
  8. Maps - Learn about storing values in the map data structure.
  9. Dependency Injection - Learn about dependency injection, how it relates to using interfaces and a primer on io.
  10. Mocking - Take some existing untested code and use DI with mocking to test it.
  11. Concurrency - Learn how to write concurrent code to make your software faster.
  12. Select - Learn how to synchronise asynchronous processes elegantly.
  13. Reflection - Learn about reflection
  14. Sync - Learn some functionality from the sync package including WaitGroup and Mutex
  15. Context - Use the context package to manage and cancel long-running processes
  16. Intro to property based tests - Practice some TDD with the Roman Numerals kata and get a brief intro to property based tests
  17. Maths - Use the math package to draw an SVG clock

Build an application

Now that you have hopefully digested the Go Fundamentals section you have a solid grounding of a majority of Go's language features and how to do TDD.

This next section will involve building an application.

Each chapter will iterate on the previous one, expanding the application's functionality as our product owner dictates.

New concepts will be introduced to help facilitate writing great code but most of the new material will be learning what can be accomplished from Go's standard library.

By the end of this, you should have a strong grasp as to how to iteratively write an application in Go, backed by tests.

  • HTTP server - We will create an application which listens to HTTP requests and responds to them.
  • JSON, routing and embedding - We will make our endpoints return JSON and explore how to do routing.
  • IO and sorting - We will persist and read our data from disk and we'll cover sorting data.
  • Command line & project structure - Support multiple applications from one code base and read input from command line.
  • Time - using the time package to schedule activities.
  • WebSockets - learn how to write and test a server that uses WebSockets.

Questions and answers

I often run in to questions on the internets like

How do I test my amazing function that does x, y and z

If you have such a question raise it as an issue on github and I'll try and find time to write a short chapter to tackle the issue. I feel like content like this is valuable as it is tackling people's real questions around testing.

  • OS exec - An example of how we can reach out to the OS to execute commands to fetch data and keep our business logic testable/
  • Error types - Example of creating your own error types to improve your tests and make your code easier to work with.

Contributing

  • This project is work in progress If you would like to contribute, please do get in touch.
  • Read contributing.md for guidelines
  • Any ideas? Create an issue

Background

I have some experience introducing Go to development teams and have tried different approaches as to how to grow a team from some people curious about Go into highly effective writers of Go systems.

What didn't work

Read the book

An approach we tried was to take the blue book and every week discuss the next chapter along with the exercises.

I love this book but it requires a high level of commitment. The book is very detailed in explaining concepts, which is obviously great but it means that the progress is slow and steady - this is not for everyone.

I found that whilst a small number of people would read chapter X and do the exercises, many people didn't.

Solve some problems

Katas are fun but they are usually limited in their scope for learning a language; you're unlikely to use goroutines to solve a kata.

Another problem is when you have varying levels of enthusiasm. Some people just learn way more of the language than others and when demonstrating what they have done end up confusing people with features the others are not familiar with.

This ends up making the learning feel quite unstructured and ad hoc.

What did work

By far the most effective way was by slowly introducing the fundamentals of the language by reading through go by example, exploring them with examples and discussing them as a group. This was a more interactive approach than "read chapter x for homework".

Over time the team gained a solid foundation of the grammar of the language so we could then start to build systems.

This to me seems analogous to practicing scales when trying to learn guitar.

It doesn't matter how artistic you think you are, you are unlikely to write good music without understanding the fundamentals and practicing the mechanics.

What works for me

When I learn a new programming language I usually start by messing around in a REPL but eventually, I need more structure.

What I like to do is explore concepts and then solidify the ideas with tests. Tests verify the code I write is correct and documents the feature I have learned.

Taking my experience of learning with a group and my own personal way I am going to try and create something that hopefully proves useful to other teams. Learning the fundamentals by writing small tests so that you can then take your existing software design skills and ship some great systems.

Who this is for

  • People who are interested in picking up Go.
  • People who already know some Go, but want to explore testing with TDD.

What you'll need

  • A computer!
  • Installed Go
  • A text editor
  • Some experience with programming. Understanding of concepts like if, variables, functions etc.
  • Comfortable with using the terminal

Feedback

MIT license

Logo is by egonelbre What a star!

You can’t perform that action at this time.