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.
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.