From cfa5d4e1747dcb5fb868d0993084e45999bbbfbc Mon Sep 17 00:00:00 2001 From: user Date: Mon, 23 Feb 2026 11:56:11 -0800 Subject: [PATCH] style(go): add Stringer rule for custom string types Custom string types should implement fmt.Stringer and use .String() instead of string() casts when passing to library code. --- prompts/CODE_STYLEGUIDE_GO.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/prompts/CODE_STYLEGUIDE_GO.md b/prompts/CODE_STYLEGUIDE_GO.md index f859589..edc94ee 100644 --- a/prompts/CODE_STYLEGUIDE_GO.md +++ b/prompts/CODE_STYLEGUIDE_GO.md @@ -429,6 +429,29 @@ last_modified: 2026-02-22 releasable". "Releasable" in this context means that it builds and functions as expected, and that all tests and linting passes. +1. Custom string types (e.g. `type ImageID string`) should implement the + `fmt.Stringer` interface by adding a `String() string` method. When passing + these types to library code or standard library functions that expect a + `string`, use the `.String()` method instead of a `string()` cast. This + keeps the code consistent with Go's interface conventions and makes it + easier to change the underlying representation later without updating every + call site. + + Example: + + ```go + type ImageID string + + // String implements fmt.Stringer. + func (id ImageID) String() string { return string(id) } + + // Right: + client.RemoveImage(ctx, imageID.String()) + + // Wrong: + client.RemoveImage(ctx, string(imageID)) + ``` + # Other Golang Tips and Best Practices (Optional) 1. For any internet-facing http server, set appropriate timeouts and limits to -- 2.45.2