Error in user YAML: (<unknown>): did not find expected alphabetic or numeric character while scanning an alias at line 2 column 8
---
trigger: glob
globs: **/*.go
---
When developing or modifying Go code in the KHI project, you must adhere to the following rules and best practices.
- Build Verification: Before running tests or submitting changes, you must always verify that your code compiles successfully.
- Run
make build-goto ensure there are no compilation errors across the backend.
- Run
- Test Verification: Run
go testwith an appropriate filter to run tests only for changed parts first. But make sure you runmake test-gobefore asking the user to verify. - Formatting and Linting:
- Run
make format-goto ensure standard Go formatting. - Run
make lint-goif applicable, and ensure no new linting errors are introduced.
- Run
- If you make any corrections during a verification phase, you MUST restart the verification from the beginning for that phase.
-
Comments:
- All comments must be written in English.
- Add GoDoc comments for all public types, functions, and methods.
- Add GoDoc comments for private types, functions, and methods when their names are not self-explanatory or usage is not intuitive.
- Add
var _ Interface = (*Implementation)(nil);after the type definition to show that it's implementing the interface explicitly.
-
Error and Test Assertion Messages:
- Do not capitalize the first character of messages in
fmt.Errorf,t.Error,t.Errorf,t.Fatal,t.Fatalf, etc. (e.g., use lowercase "failed to ..." instead of "Failed to ..."). - This rule does not apply if the message starts with a capitalized name, such as a method name or proper acronym (e.g.
MyFunction() mismatch (-want +got)).
- Do not capitalize the first character of messages in
- Table-Driven Tests: Tests must be written using the table-driven testing pattern. Define a slice of anonymous structs representing the test cases, and iterate over them using
t.Run(). - Assertions and Diffs:
- MUST USE
github.com/google/go-cmp/cmpfor complex comparisons and generating diffs. Showcmp.Diffwhen an assertion fails to clearly communicate the mismatch. - DO NOT USE the
reflectpackage for test assertions (e.g.,reflect.DeepEqual). Always prefercmp.Diff.
- MUST USE
- Running Tests:
- Executing
make test-goruns all backend tests. - For iterating on specific tests,
go test ./pkg/path/to/test -run TestNameis acceptable, provided a fullmake test-goensures no regressions before finalizing work.
- Executing
- Test File Naming: When adding tests for a file
A.go, the test file must be namedA_test.go. Do not create independent test files that group tests from multiple files.
Important
A typical table-driven test should look something like this:
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestMyFunction(t *testing.T) {
testCases := []struct {
name string
input string
want string
}{
{
name: "valid input",
input: "foo",
want: "bar",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := MyFunction(tc.input)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("MyFunction() mismatch (-want +got):\n%s", diff)
}
})
}
}