Top Related Projects
Standard Go Project Layout
A curated list of awesome Go frameworks, libraries and software
❤️ 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. 🚀 Learn Go by fixing 1000+ tiny programs.
Training for Golang (go language)
Curated list of project-based tutorials
A golang ebook intro how to build a web with golang
Quick Overview
"Learn Go with Tests" is an open-source project that provides a hands-on approach to learning Go programming through test-driven development (TDD). It offers a series of tutorials and exercises that guide learners through various Go concepts while emphasizing the importance of writing tests alongside code.
Pros
- Combines learning Go with best practices in test-driven development
- Provides practical, hands-on exercises for better retention of concepts
- Covers a wide range of Go topics, from basics to advanced concepts
- Regularly updated and maintained by the community
Cons
- May be challenging for complete beginners to programming
- Requires a commitment to learning both Go and TDD simultaneously
- Some advanced topics might need additional resources for deeper understanding
- Progress can be slower compared to traditional learning methods due to the focus on writing tests
Code Examples
Here are a few examples from the project:
- Basic function and test:
// hello.go
func Hello(name string) string {
return "Hello, " + name
}
// hello_test.go
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
- Struct method and table-driven test:
// shapes.go
type Rectangle struct {
Width float64
Height float64
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
// shapes_test.go
func TestArea(t *testing.T) {
areaTests := []struct {
name string
shape Shape
hasArea float64
}{
{name: "Rectangle", shape: Rectangle{Width: 12, Height: 6}, hasArea: 72.0},
{name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793},
}
for _, tt := range areaTests {
t.Run(tt.name, func(t *testing.T) {
got := tt.shape.Area()
if got != tt.hasArea {
t.Errorf("%#v got %g want %g", tt.shape, got, tt.hasArea)
}
})
}
}
Getting Started
To get started with "Learn Go with Tests":
- Install Go on your system (https://golang.org/doc/install)
- Clone the repository:
git clone https://github.com/quii/learn-go-with-tests.git
- Navigate to a chapter directory and run the tests:
cd learn-go-with-tests/hello-world go test
- Follow the instructions in the README.md file for each chapter to progress through the tutorials.
Competitor Comparisons
Standard Go Project Layout
Pros of project-layout
- Provides a standardized structure for Go projects, making it easier for developers to navigate and understand large codebases
- Includes examples and explanations for various project components, such as configs, internal packages, and build files
- Offers a comprehensive template that can be used as a starting point for new Go projects
Cons of project-layout
- May be overwhelming for beginners or small projects, as it includes many directories and files that might not be necessary
- Lacks hands-on learning exercises and test-driven development approach found in learn-go-with-tests
- Does not provide in-depth explanations of Go concepts and best practices
Code comparison
learn-go-with-tests:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
project-layout:
// cmd/app/main.go
package main
import (
"log"
"github.com/your_username/project_name/internal/app/myapp"
)
func main() {
if err := myapp.Run(); err != nil {
log.Fatal(err)
}
}
A curated list of awesome Go frameworks, libraries and software
Pros of awesome-go
- Comprehensive collection of Go resources, libraries, and tools
- Regularly updated with community contributions
- Covers a wide range of topics and categories
Cons of awesome-go
- Lacks structured learning path for beginners
- No hands-on coding exercises or tutorials
- May be overwhelming for newcomers due to the sheer volume of information
Code comparison
learn-go-with-tests:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
awesome-go:
// No direct code examples provided
// The repository is a curated list of resources
Summary
learn-go-with-tests is a structured, hands-on learning resource focused on test-driven development in Go. It provides step-by-step tutorials and coding exercises, making it ideal for beginners and those looking to improve their Go skills through practical application.
awesome-go, on the other hand, is a comprehensive collection of Go-related resources, libraries, and tools. It serves as an excellent reference for developers of all levels, offering a wide range of options for various Go-related tasks and projects. However, it doesn't provide a structured learning path or hands-on exercises like learn-go-with-tests does.
❤️ 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. 🚀 Learn Go by fixing 1000+ tiny programs.
Pros of learngo
- More comprehensive coverage of Go concepts, including advanced topics
- Includes interactive coding exercises and quizzes
- Offers a structured learning path with clear progression
Cons of learngo
- Less emphasis on test-driven development (TDD) practices
- May be overwhelming for absolute beginners due to its extensive content
- Lacks the strong focus on writing tests alongside code
Code Comparison
learn-go-with-tests:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
learngo:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
The code comparison shows that learn-go-with-tests emphasizes writing tests from the beginning, while learngo focuses on introducing basic Go syntax and concepts. learn-go-with-tests encourages a TDD approach, whereas learngo provides a more traditional learning experience with explanations followed by code examples.
Both repositories offer valuable resources for learning Go, but they cater to different learning styles and objectives. learn-go-with-tests is ideal for those who want to learn Go while adopting TDD practices, while learngo provides a comprehensive curriculum covering a wide range of Go topics.
Training for Golang (go language)
Pros of GolangTraining
- Covers a broader range of Go topics, including more advanced concepts
- Includes practical examples and exercises for real-world scenarios
- Offers video tutorials alongside written content for enhanced learning
Cons of GolangTraining
- Less structured approach compared to learn-go-with-tests
- May not emphasize test-driven development as strongly
- Some content might be outdated due to less frequent updates
Code Comparison
learn-go-with-tests example:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
GolangTraining example:
func main() {
fmt.Println("Hello, World!")
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
The learn-go-with-tests example demonstrates a test-driven approach, while the GolangTraining example shows a basic program structure. learn-go-with-tests focuses on writing tests first, whereas GolangTraining provides more general Go programming examples.
Both repositories offer valuable resources for learning Go, with learn-go-with-tests emphasizing test-driven development and GolangTraining covering a wider range of topics. The choice between them depends on the learner's goals and preferred learning style.
Curated list of project-based tutorials
Pros of project-based-learning
- Covers a wide range of programming languages and technologies
- Provides a comprehensive list of project ideas for various skill levels
- Offers a diverse set of resources, including tutorials, articles, and courses
Cons of project-based-learning
- Lacks a structured learning path for specific languages or technologies
- May not provide in-depth explanations or step-by-step guidance for each project
- Quality and consistency of resources can vary as they are curated from different sources
Code comparison
Not applicable, as project-based-learning is a curated list of resources and does not contain code samples directly in the repository.
learn-go-with-tests, on the other hand, provides code examples for each lesson. Here's a sample from the Hello World section:
func Hello(name string) string {
if name == "" {
name = "World"
}
return "Hello, " + name
}
Summary
project-based-learning offers a broad collection of project ideas and resources across multiple languages, making it suitable for developers looking to explore various technologies. learn-go-with-tests focuses specifically on Go programming with a test-driven approach, providing a more structured and in-depth learning experience for Go enthusiasts.
A golang ebook intro how to build a web with golang
Pros of build-web-application-with-golang
- Comprehensive coverage of web development topics in Go
- Includes practical examples for building web applications
- Available in multiple languages, making it accessible to a wider audience
Cons of build-web-application-with-golang
- Less focus on test-driven development (TDD) practices
- May not be as up-to-date with the latest Go features and best practices
- Lacks interactive exercises for hands-on learning
Code Comparison
build-web-application-with-golang:
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello myroute!")
}
func main() {
http.HandleFunc("/", sayhelloName)
http.ListenAndServe(":9090", nil)
}
learn-go-with-tests:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
The code snippets highlight the different approaches: build-web-application-with-golang focuses on web application structure, while learn-go-with-tests emphasizes test-driven development. The former demonstrates setting up a basic web server, while the latter shows how to write and run tests for Go functions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Learn Go with Tests
Formats
Translations
Support me
I am proud to offer this resource for free, but if you wish to give some appreciation:
Why
- Explore the Go language by writing tests
- Get a grounding with TDD. Go is a good language for learning TDD because it is a simple language to learn and testing is built-in
- Be confident that you'll be able to start writing robust, well-tested systems in Go
- Watch a video, or read about why unit testing and TDD is important
Table of contents
Go fundamentals
- Install Go - Set up environment for productivity.
- Hello, world - Declaring variables, constants, if/else statements, switch, write your first go program and write your first test. Sub-test syntax and closures.
- Integers - Further Explore function declaration syntax and learn new ways to improve the documentation of your code.
- Iteration - Learn about
for
and benchmarking. - Arrays and slices - Learn about arrays, slices,
len
, varargs,range
and test coverage. - Structs, methods & interfaces - Learn about
struct
, methods,interface
and table driven tests. - Pointers & errors - Learn about pointers and errors.
- Maps - Learn about storing values in the map data structure.
- Dependency Injection - Learn about dependency injection, how it relates to using interfaces and a primer on io.
- Mocking - Take some existing untested code and use DI with mocking to test it.
- Concurrency - Learn how to write concurrent code to make your software faster.
- Select - Learn how to synchronise asynchronous processes elegantly.
- Reflection - Learn about reflection
- Sync - Learn some functionality from the sync package including
WaitGroup
andMutex
- Context - Use the context package to manage and cancel long-running processes
- Intro to property based tests - Practice some TDD with the Roman Numerals kata and get a brief intro to property based tests
- Maths - Use the
math
package to draw an SVG clock - Reading files - Read files and process them
- Templating - Use Go's html/template package to render html from data, and also learn about approval testing
- Generics - Learn how to write functions that take generic arguments and make your own generic data-structure
- Revisiting arrays and slices with generics - Generics are very useful when working with collections. Learn how to write your own
Reduce
function and tidy up some common patterns.
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.
Testing fundamentals
Covering other subjects around testing.
- Introduction to acceptance tests - Learn how to write acceptance tests for your code, with a real-world example for gracefully shutting down a HTTP server
- Scaling acceptance tests - Learn techniques to manage the complexity of writing acceptance tests for non-trivial systems.
- Working without mocks, stubs and spies - Learn about how to use fakes and contracts to create more realistic and maintainable tests.
- Refactoring Checklist - Some discussion on what refactoring is, and some basic tips on how to do it.
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.
- Context-aware Reader - Learn how to TDD augmenting
io.Reader
with cancellation. Based on Context-aware io.Reader for Go - Revisiting HTTP Handlers - Testing HTTP handlers seems to be the bane of many a developer's existence. This chapter explores the issues around designing handlers correctly.
Meta / Discussion
- Why unit tests and how to make them work for you - Watch a video, or read about why unit testing and TDD is important
- Anti-patterns - A short chapter on TDD and unit testing anti-patterns
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 using the terminal
Feedback
- Add issues/submit PRs here or tweet me @quii
Logo is by egonelbre What a star!
Top Related Projects
Standard Go Project Layout
A curated list of awesome Go frameworks, libraries and software
❤️ 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. 🚀 Learn Go by fixing 1000+ tiny programs.
Training for Golang (go language)
Curated list of project-based tutorials
A golang ebook intro how to build a web with golang
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot