Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Azure Developer CLI (azd) - Instructions for Copilot Chat and Copilot code review

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.
42 changes: 40 additions & 2 deletions cli/azd/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,51 @@ This project uses Go 1.26. Use modern standard library features:
- **Range over integers**: `for i := range 10 { }`

### Modern Go Patterns (Go 1.26+)
- Use `new(val)` not `x := val; &x` - returns pointer to any value.
Go 1.26 extends `new()` to accept expressions, not just types.
Type is inferred: `new(0) → *int`, `new("s") → *string`, `new(T{}) → *T`.
DO NOT use `x := val; &x` pattern — always use `new(val)` directly.
DO NOT use redundant casts like `new(int(0))` — just write `new(0)`.
Before:
```go
timeout := 30
debug := true
cfg := Config{
Timeout: &timeout,
Debug: &debug,
}
```

After:
```go
cfg := Config{
Timeout: new(30), // *int
Debug: new(true), // *bool
}
```

- Use `errors.AsType[T](err)` not `errors.As(err, &target)`.
ALWAYS use errors.AsType when checking if error matches a specific type.

Before:
```go
var pathErr *os.PathError
if errors.As(err, &pathErr) {
handle(pathErr)
}
```

After:
```go
if pathErr, ok := errors.AsType[*os.PathError](err); ok {
handle(pathErr)
}
```

- Use `errors.AsType[*MyError](err)` instead of `var e *MyError; errors.As(err, &e)`
- Use `slices.SortFunc(items, func(a, b T) int { return cmp.Compare(a.Name, b.Name) })` instead of `sort.Slice`
- Use `slices.Clone(s)` instead of `append([]T{}, s...)`
- Use `slices.Sorted(maps.Keys(m))` instead of collecting keys and sorting them separately
- Use `http.NewRequestWithContext(ctx, method, url, body)` instead of `http.NewRequest(...)`
- Use `new(expr)` instead of `to.Ptr(expr)`; `go fix ./...` applies this automatically
- Use `wg.Go(func() { ... })` instead of `wg.Add(1); go func() { defer wg.Done(); ... }()`
- Use `for i := range n` instead of `for i := 0; i < n; i++` for simple counted loops
- Use `t.Context()` instead of `context.Background()` in tests
Expand Down
Loading