Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

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

WHY

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

WHAT
  • Go: download from go.dev. Check with go version.
  • Built-in tools: go test, go vet and gofmt come with Go.
  • IDE + assistant: VS Code with the Go extension + Copilot or Claude Code, or GoLand with Copilot.

How it flows

1 · Describe the function std lib only, examples 2 · AI writes code read every line 3 · gofmt + go vet clean 4 · Ask for table tests many cases, one loop 5 · go test PASS = keep

Install and set up

HOW
  1. Install Go from go.dev/dl.
  2. Check: go version.
  3. Make a module:
mkdir ifsc && cd ifsc
go mod init ifsc

Step 1 — the prompt

COPY THIS 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

FOLLOW-UP PROMPT

Write a table-driven Go test for IsValid with valid, lowercase-with-spaces, wrong 5th character, too short and empty cases.

ifsc_test.go
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  ifsc
Note: this checks the format only. It does not prove the branch exists. For that you need a bank or RBI data source.

Settings and config

  1. VS Code: install the Go extension; it runs gofmt on save and offers to install gopls.
  2. Rules file: “Go 1.22+, standard library first, table tests, wrap errors with fmt.Errorf("...: %w", err)”.
  3. Run go vet ./... and go 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 -race for 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

SMART AI USE

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.

Save these cards

INTERVIEW CARDAI Coding

How do you check AI Go code?

gofmt, go vet, then table-driven go test.
Handle every error; -race for goroutines.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Go tests vs other languages

Go: testing package built in, no install.
Java/C#: add JUnit or xUnit.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Go with AI in 3

Std lib only + examples in the prompt.
Table tests.
go vet + go test = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding