How to Code in Go Using AI
Use an AI assistant to write a Go function, run it, and prove it with Go's built-in table tests. Go needs no extra test library.
In short: say the Go version and “standard library only”, give examples, ask for a table-driven test, run go test and go vet.
Why Go with AI
Go runs cloud tools and backends at companies like Google and Uber. Docker and Kubernetes are written in Go. The language is small and strict, so AI code is easy to check with go vet and tests.
What you need
- Go: download from go.dev. Check with
go version. - Built-in tools:
go test,go vetandgofmtcome with Go. - IDE + assistant: VS Code with the Go extension + Copilot or Claude Code, or GoLand with Copilot.
How it flows
Install and set up
- Install Go from go.dev/dl.
- Check:
go version. - Make a module:
mkdir ifsc && cd ifsc go mod init ifsc
Step 1 — the prompt
Write a Go package ifsc with IsValid(code string) bool that checks an Indian bank IFSC code: 4 letters, then the digit 0, then 6 letters or digits. Ignore spaces around it and allow lowercase. Standard library only. Example: "SBIN0001234" is valid, "SBIN1001234" is not. Only return the code.
Step 2 — the code (tested here)
ifsc.go// Package ifsc checks Indian bank IFSC codes. Built with an AI assistant, then tested.
package ifsc
import (
"regexp"
"strings"
)
// Format: 4 letters (bank), the digit 0, then 6 letters or digits (branch).
var pattern = regexp.MustCompile(`^[A-Z]{4}0[A-Z0-9]{6}$`)
// IsValid reports whether code looks like a valid IFSC code.
// Spaces around the code are ignored and lowercase is allowed.
func IsValid(code string) bool {
return pattern.MatchString(strings.ToUpper(strings.TrimSpace(code)))
}Step 3 — ask for a table test, then run it
Write a table-driven Go test for IsValid with valid, lowercase-with-spaces, wrong 5th character, too short and empty cases.
package ifsc
import "testing"
func TestIsValid(t *testing.T) {
cases := []struct {
in string
want bool
}{
{"SBIN0001234", true},
{" hdfc0000123 ", true},
{"SBIN1001234", false}, // 5th char must be 0
{"SBI0001234", false}, // too short
{"", false},
}
for _, c := range cases {
if got := IsValid(c.in); got != c.want {
t.Errorf("IsValid(%q) = %v, want %v", c.in, got, c.want)
}
}
}go vet ./...
go test -v ./...
# -> --- PASS: TestIsValid
# -> ok ifscSettings and config
- VS Code: install the Go extension; it runs gofmt on save and offers to install gopls.
- Rules file: “Go 1.22+, standard library first, table tests, wrap errors with fmt.Errorf("...: %w", err)”.
- Run
go vet ./...andgo test ./...in CI on every pull request.
Watch out (common AI mistakes in Go)
- Ignored errors —
x, _ := ...; ask it to handle every error. - Made-up packages — check on pkg.go.dev.
- Goroutine leaks and data races — run
go test -racefor concurrent code. - Compiling a regex inside a loop — it should be a package-level variable, like above.
Try this yourself: ask AI for a Go shipping band, then prove it with table tests
Hello friends. The live chapter already walked through IFSC IsValid with a table test. This practice is a different fictional shop called Drift Pine Outfitters. You ask an AI for a Go helper Band that maps parcel weight in kilograms to Local, Regional, or National, rejects bad weights with clear errors, and proves every cutoff with go test. On this drafting machine we ran the real go 1.27.2 toolchain: the starter failed 5 subtests and passed 4, and the fixed package passed all 9 subtests.
नमस्कार मित्रांनो. Live chapter मध्ये IFSC IsValid आणि table test आधीच आले आहे. हा सराव Drift Pine Outfitters नावाच्या वेगळ्या काल्पनिक दुकानावर आहे. तुम्ही AI कडून Band हा Go helper मागता जो किलोग्रॅममधील वजन Local, Regional किंवा National मध्ये मॅप करतो, चुकीच्या वजनाला स्पष्ट error देतो, आणि प्रत्येक cutoff go test ने सिद्ध करतो. या drafting मशीनवर आम्ही खरी go 1.27.2 toolchain चालवली: starter मध्ये 5 subtests fail आणि 4 pass झाले, आणि fixed package मध्ये सर्व 9 subtests पास झाले.
नमस्ते दोस्तों. Live chapter में IFSC IsValid और table test पहले आ चुके हैं. यह अभ्यास Drift Pine Outfitters नाम की अलग काल्पनिक दुकान पर है. आप AI से Band नाम का Go helper मांगते हो जो किलोग्राम में वजन को Local, Regional या National में मैप करे, गलत वजन पर साफ़ error दे, और हर cutoff को go test से साबित करे. इस drafting मशीन पर हमने असली go 1.27.2 toolchain चलाई: starter में 5 subtests fail और 4 pass हुए, और fixed package में सभी 9 subtests पास हुए।
Ravindra Bagale's Tip
Put the exact band cutoffs and the error message substrings in your prompt. Ask for table-driven tests with t.Run names you can read in the failure list. Run go test on the starter first so you see which cutoffs still fail, then ask the AI to fix only those cases.
Ravindra Bagale's Tip – मराठी
Prompt मध्ये exact band cutoffs आणि error message substrings लिहा. t.Run नावांसह table-driven tests मागा जे failure list मध्ये वाचता येतील. आधी starter वर go test चालवा जेणेकरून कुठले cutoffs अजून fail होतात दिसेल, नंतर AI ला फक्त तेच cases सुधारायला सांगा.
Ravindra Bagale's Tip – हिंदी
Prompt में exact band cutoffs और error message substrings लिखो. ऐसे table-driven tests मांगो जिनके t.Run नाम failure list में पढ़े जा सकें. पहले starter पर go test चलाओ ताकि दिखे कौन से cutoffs अभी fail हो रहे हैं, फिर AI से सिर्फ वही cases ठीक करवाओ।
go 1.27.2 linux/amd64 at /usr/local/bin/go. Starter go test reported 5 failed subtests and 4 passed subtests. Fixed go test reported PASS with 9 passed subtests. No new Go toolchain was installed for this draft.Your task: start from the buggy shipping.go that returns wrong bands and skips validation. Ask an AI to match the Local / Regional / National cutoffs, reject non-positive weights and weights above 30 kg, and keep table-driven tests in shipping_test.go.
Setup
- Create a project folder and open a terminal inside it.
- Create
go.modwith module pathdriftpine.example/shippingand a Go version your machine supports. - Save
shipping.goandshipping_test.goin the same folder. - Confirm the toolchain: run
go versionand expect a Go 1.22 or newer release.
Starter go.mod:
module driftpine.example/shipping
go 1.22
Starter shipping.go (wrong cutoffs, skips validation):
// Package shipping assigns Drift Pine Outfitters shipping bands by parcel weight.
// Fictional teaching shop only — not a real carrier or price list.
// Starter: returns a label for a few ranges but skips validation and wrong bands.
package shipping
// Band returns a shipping band label for weightKg.
// Expected bands after the fix:
// 0 < kg <= 2 -> "Local"
// 2 < kg <= 10 -> "Regional"
// 10 < kg <= 30 -> "National"
// Rejects non-positive weight and weight above 30.
func Band(weightKg float64) (string, error) {
// Starter bugs: no validation; wrong cutoffs; empty string for many inputs.
if weightKg <= 5 {
return "Local", nil
}
if weightKg <= 20 {
return "Regional", nil
}
return "National", nil
}
Table tests shipping_test.go:
package shipping
import (
"strings"
"testing"
)
func TestBandTable(t *testing.T) {
cases := []struct {
name string
kg float64
want string
errPart string
}{
{"local_one_point_five", 1.5, "Local", ""},
{"local_exact_two", 2.0, "Local", ""},
{"regional_two_point_one", 2.1, "Regional", ""},
{"regional_ten", 10.0, "Regional", ""},
{"national_ten_point_one", 10.1, "National", ""},
{"national_thirty", 30.0, "National", ""},
{"rejects_zero", 0, "", "positive"},
{"rejects_negative", -1, "", "positive"},
{"rejects_over_thirty", 30.1, "", "30 kg"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := Band(tc.kg)
if tc.errPart == "" {
if err != nil {
t.Fatalf("Band(%v) unexpected error: %v", tc.kg, err)
}
if got != tc.want {
t.Fatalf("Band(%v) = %q, want %q", tc.kg, got, tc.want)
}
return
}
if err == nil {
t.Fatalf("Band(%v) expected error containing %q, got nil (label %q)", tc.kg, tc.errPart, got)
}
if !strings.Contains(err.Error(), tc.errPart) {
t.Fatalf("Band(%v) error %q, want substring %q", tc.kg, err.Error(), tc.errPart)
}
})
}
}
Suggested AI ask (copy and adapt):
go-prompt.txtWrite a Go 1.22 package shipping with func Band(weightKg float64) (string, error) for fictional Drift Pine Outfitters. Reject NaN and Inf with an error mentioning finite. Reject weightKg <= 0 with an error mentioning positive. Reject weightKg > 30 with an error mentioning 30 kg. Return Local for 0 < kg <= 2, Regional for 2 < kg <= 10, and National for 10 < kg <= 30. Add table-driven tests in shipping_test.go covering the cutoffs and the three error cases.
HOW to finish
- Save the starter files in one module folder.
- Ask your AI assistant to fix the cutoffs and add validation with the error substrings the tests search for.
- Run
go test. On the buggy starter this drafting box saw 5 failed and 4 passed subtests. - Run again after the fix. Expect PASS with 9 passed subtests.
Real go test output after the fixed package:
PASS
ok driftpine.example/shipping 0.002s
Optional completed shipping.go (try it yourself first)
Your AI assistant may reorder the checks. Any version is fine if go test passes all nine subtests.
// Package shipping assigns Drift Pine Outfitters shipping bands by parcel weight.
// Fictional teaching shop only — not a real carrier or price list.
package shipping
import (
"errors"
"math"
)
// Band returns a shipping band label for weightKg.
// 0 < kg <= 2 -> "Local"
// 2 < kg <= 10 -> "Regional"
// 10 < kg <= 30 -> "National"
func Band(weightKg float64) (string, error) {
if math.IsNaN(weightKg) || math.IsInf(weightKg, 0) {
return "", errors.New("weight must be a finite number")
}
if weightKg <= 0 {
return "", errors.New("weight must be positive")
}
if weightKg > 30 {
return "", errors.New("weight must not exceed 30 kg")
}
if weightKg <= 2 {
return "Local", nil
}
if weightKg <= 10 {
return "Regional", nil
}
return "National", nil
}
Troubleshooting
- Problem:
Band(2.1)still returnsLocal. Fix: Local must end at exactly 2.0 inclusive, so values above 2.0 belong to Regional. - Problem:
Band(0)returns a label instead of an error. Fix: reject every weight that is not greater than zero, and put the wordpositivein the error message. - Error:
go: cannot find main module. Fix: creatego.modin the same folder asshipping.gobefore you rungo test. - Problem: the over-30 case fails because the message does not match. Fix: include the substring
30 kgin that error text so the table test can find it.
Practice cards: English and spoken Marathi
Each card below explains one idea on its own, so you can save or revise a single card without reading the whole chapter.
Name the cutoffs in the prompt
Keep error substrings stable
Prompt मध्ये cutoffs सांगा
Error substrings स्थिर ठेवा
Quick quiz
1. Which command runs Go tests?
go test ./....
2. What is a table-driven test?
A list of input/expected cases checked in one loop.
3. Is "SBIN1001234" a valid IFSC format?
No. The 5th character must be 0.
4. Which flag finds data races?
go test -race.
Save tokens and money
Go's own tools (gofmt, go vet, go test) are free and fast. Run them first and send the AI only the real failure lines. Ask for one function plus its table test at a time.