iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💡

Implementing a Go API with Claude Code: Surprising Autonomy and Implementation Skills

に公開

Introduction

I recently tried implementing a REST API in Go using the much-talked-about Claude Code.
Based on a Go learning project I created a few years ago, I asked Claude Code for various improvements, and I was amazed by its autonomy and implementation capabilities.

In this article, I will share my experience using Claude Code to perform the following tasks:

  • Creating a MySQL container and initial tables using Docker
  • Automatic generation of README.md
  • Implementing an API to retrieve all records from a table
  • Improving project structure
  • Implementing unit tests

Setting up MySQL environment with Docker

First, I made a request with a simple prompt like this:

Please create a MySQL container using Docker and docker compose. Also, create a books table as an initial table and prepare initial data. The table structure should follow domain/model/book.go. Also, change docker-compose.yml to compose.yml.

Points that impressed me

Autonomously resolved errors

When it failed to start via docker compose up, Claude Code autonomously identified the problem and fixed it.

  • Changed the repository for the air dependency
  • Upgraded the Go version

Thoughtful initial data

ryThe contents of the records in the initial table were related to Go and MySQL. I felt it was considering the context of a learning project.

Generation of appropriate commit messages

It automatically generated concise commit messages that matched the changes.

Actual commit: 7afd9bc

Automatic generation of README.md

Next, I asked it to create a README for the project.

Analyze the project content and create a README.md

Points that impressed me

Description based on understanding of the architecture

It sensed that the project was built with Clean Architecture and described it as such in the README.md.

Comprehensive coverage of necessary information

It appropriately listed necessary information such as the tech stack, project structure, architecture, how to start the app, curl commands for operation verification, and database information.

Generated README.md

https://github.com/yoshi-koyama/go-bookstore-api/blob/2248111ebb26300b56b5f88cc28b5d4f0ee4a5e0/README.md

Points that needed improvement

There was an error in the curl command for operation verification, so I fixed it with the following prompt:

The JSON in the curl -X POST http://localhost:8080/bookstore/api/checkouts command is incorrect. Please fix it based on the contents of handler/request/book.go.

Actual commits:
b10d121
b03fa9b

Implementing the Book List Retrieval API

Since the existing project didn't have an API to retrieve books, I requested a new implementation.

I want you to implement an API that retrieves a list of books at /books

Points that impressed me

Implementation following Clean Architecture

It understood the architecture patterns of the existing code and implemented it accordingly.

Step-by-step implementation and confirmation

Instead of proceeding with the implementation all at once, it progressed layer by layer, such as domain/model, domain/repository, infra, and usecase, asking for confirmation at each step.

Verification of operation

It created curl commands for operation verification and confirmed that the API actually worked.

Actual commit: 222c73e

Excerpts of the interaction with Claude Code


Planning


Domain implementation


Usecase implementation


Verification performed automatically

Adding database access

Since the initial implementation returned sample data, I requested a change to retrieve from MySQL.

Regarding the /books API created earlier, I'd like to change it to retrieve the list from the MySQL books table. Let's use https://github.com/jmoiron/sqlx for database access

Points that impressed me

Automatically added necessary dependencies

Since both sqlx and a MySQL driver were needed, it added them appropriately.

DAO implementation as imagined

There were no issues with the DAO modification, and the implementation was exactly as I imagined.

Autonomous resolution of character encoding issues

Japanese character encoding issues occurred, but it performed several fixes autonomously and brought them to a resolution.

Actual commit: 459fc90

Excerpts of the interaction with Claude Code


Adding sqlx and MySQL driver


DAO implementation fix was also perfect


Encountered character encoding issues during verification but resolved them autonomously


Specifying character set in DSN


Changing database character set settings


Character encoding issue successfully resolved

Improving project structure

I had it review the structure of the existing project.

I want to review the project structure

Proposed improvements

  1. Module name: dependency-injection-sample → A more specific name (e.g., bookstore-api)
  2. Configuration management: Reading environment variables directly → Centralized management with a config package
  3. Entry point: main.go → cmd/api/main.go (supporting multiple commands)
  4. Documentation: Adding a docs/ directory
  5. Testing: Adding test files
  6. Common libraries: pkg/ directory (error handling, loggers, etc.)

It presented several improvement ideas that I hadn't even noticed myself.

Relocating main.go and configuration management

Let's place main.go in cmd/api. Also, let's create config.go to read environment variables

In connection with the relocation of main.go, it also appropriately modified .air.toml.

Actual commit: 949a93a

Excerpts of the interaction with Claude Code


Creating config.go


Changing db.go to use config


Modifying .air.toml according to the relocation of main.go

Renaming the project

Let's review the project name. Please suggest some candidates.

Suggested candidates

Simple style:

  1. bookstore-api ⭐️ Recommended
  2. go-bookstore
  3. bookstore

Detailed style:
4. bookstore-rest-api
5. go-bookstore-api

Domain-related style:
6. bookshop-api
7. litstore-api

I adopted the recommended proposal as it was the most appropriate.

Let's go with bookstore-api

It scanned the necessary locations and performed a bulk conversion. It was fast and accurate.

Actual commit: 1bdcd4e

Implementing unit tests

Finally, I requested the creation of unit tests. In Claude Code, you can specify files using @.

Please create unit tests for @domain/model/book.go

Points that impressed me

Coverage of edge cases

It also tested edge cases, such as the price being 0 or the book title being very long.

If there were validation implementations, it seems like it would handle boundary value testing as well.

https://github.com/yoshi-koyama/go-bookstore-api/blob/2248111ebb26300b56b5f88cc28b5d4f0ee4a5e0/domain/model/book_test.go#L20-L33

Actual commit: 2c51016

Cost

In addition to the tasks introduced in this article, I had it perform several other operations, and it cost $7.29 within about 30 minutes of starting use.


/cost command for displaying cost

Summary

I used Claude Code to refactor an existing Go API and implement a new API.

Strengths of Claude Code

Depth of context understanding

Even with simple prompts, it deciphered the context of the source code and autonomously performed the necessary modifications with a reasonably fine granularity.

Autonomous problem solving

When errors occurred, it identified the cause and led the way to a resolution while attempting multiple fixes.

Understanding and respecting the architecture

It understood and followed the existing codebase's architecture patterns, such as Clean Architecture, during implementation.

Use Cases

Simple tasks can be completed just by requesting them from Claude Code. Furthermore, since it grasps the contents of the project well, it could also be used to deepen your understanding of the project through dialogue.

I felt that Claude Code is a tool that can be a powerful partner in coding work.

Discussion