Skip to content

Commit c1e215e

Browse files
committed
Fix Copilot instructions for code review and strengthen guidance on Go patterns (#7320)
* Fix Copilot instructions for code review and strengthen guidance on Go patterns * Update wording
1 parent b696e44 commit c1e215e

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

.github/copilot-instructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Azure Developer CLI (azd) - Instructions for Copilot Chat and Copilot code review
2+
3+
For any work in this repository, especially for code reviews, you MUST read [cli/azd/AGENTS.md](../cli/azd/AGENTS.md) in its entirety (every line) first.

cli/azd/AGENTS.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,51 @@ This project uses Go 1.26. Use modern standard library features:
205205
- **Range over integers**: `for i := range 10 { }`
206206
207207
### Modern Go Patterns (Go 1.26+)
208+
- Use `new(val)` not `x := val; &x` - returns pointer to any value.
209+
Go 1.26 extends `new()` to accept expressions, not just types.
210+
Type is inferred: `new(0) → *int`, `new("s") → *string`, `new(T{}) → *T`.
211+
DO NOT use `x := val; &x` pattern — always use `new(val)` directly.
212+
DO NOT use redundant casts like `new(int(0))` — just write `new(0)`.
213+
Before:
214+
```go
215+
timeout := 30
216+
debug := true
217+
cfg := Config{
218+
Timeout: &timeout,
219+
Debug: &debug,
220+
}
221+
```
222+
223+
After:
224+
```go
225+
cfg := Config{
226+
Timeout: new(30), // *int
227+
Debug: new(true), // *bool
228+
}
229+
```
230+
231+
- Use `errors.AsType[T](err)` not `errors.As(err, &target)`.
232+
ALWAYS use errors.AsType when checking if error matches a specific type.
233+
234+
Before:
235+
```go
236+
var pathErr *os.PathError
237+
if errors.As(err, &pathErr) {
238+
handle(pathErr)
239+
}
240+
```
241+
242+
After:
243+
```go
244+
if pathErr, ok := errors.AsType[*os.PathError](err); ok {
245+
handle(pathErr)
246+
}
247+
```
208248
209-
- Use `errors.AsType[*MyError](err)` instead of `var e *MyError; errors.As(err, &e)`
210249
- Use `slices.SortFunc(items, func(a, b T) int { return cmp.Compare(a.Name, b.Name) })` instead of `sort.Slice`
211250
- Use `slices.Clone(s)` instead of `append([]T{}, s...)`
212251
- Use `slices.Sorted(maps.Keys(m))` instead of collecting keys and sorting them separately
213252
- Use `http.NewRequestWithContext(ctx, method, url, body)` instead of `http.NewRequest(...)`
214-
- Use `new(expr)` instead of `to.Ptr(expr)`; `go fix ./...` applies this automatically
215253
- Use `wg.Go(func() { ... })` instead of `wg.Add(1); go func() { defer wg.Done(); ... }()`
216254
- Use `for i := range n` instead of `for i := 0; i < n; i++` for simple counted loops
217255
- Use `t.Context()` instead of `context.Background()` in tests

0 commit comments

Comments
 (0)