# if got, want: A Simple Way to Write Better Go Tests

DevFeed: [if got, want: A Simple Way to Write Better Go Tests](<https://devfeed.tech/articles/if-got-want-a-simple-way-to-write-better-go-tests-52783.md>)

Original publisher: [Read original article](<https://mtlynch.io/if-got-want-improve-go-tests/>)

Author: Michael Lynch

Published: 2025-01-08T00:00:00Z

Content type: tutorial

Language: en

Sources: [Mtlynch](<https://devfeed.tech/sources/mtlynch-io.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [table driven tests](<https://devfeed.tech/topics/table-driven-tests.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [go](<https://devfeed.tech/tags/go.md>), [table-driven-tests](<https://devfeed.tech/tags/table-driven-tests.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

A tutorial on using the `if got, want :=` pattern in Go tests. The pattern makes expected and actual values clearer, simplifies copied assertions, and separates assertions from test control flow.

## Source excerpt

There's an excellent Go testing pattern that too few people know. I can teach it to you in 30 seconds. Instead of writing Go tests like this: // The common, unrefined way. username := GetUser() if username != "dummyUser" { t.Errorf("unexpected username: got %s, want: %s", username, "dummyUser") } Write your tests like this, beginning each assertion with if got, want :=: // The underused, elegant way. if got, want := GetUser(), "dummyUser"; got != want { t.Errorf("username=%s, want=%s", got, want) } The if got, want :=: pattern works even better in table-driven tests. Here's an example from my library for parsing social media handles: