Skip to content

Commit 55ddf85

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
docs(general-patterns): fix constant comparison pattern
Use int(returnVal) to match int-typed constants, following the convention established in telephony.md and other existing examples.
1 parent e4e388e commit 55ddf85

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

docs/general-patterns.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ func example(ctx *app.Context) error {
3434
return err
3535
}
3636

37-
// Use named constants -- never magic numbers
38-
switch phoneType {
39-
case int32(telephony.PhoneTypeGsm):
37+
// Use named constants -- never magic numbers.
38+
// Constants are typed int; cast the int32 return to match.
39+
switch int(phoneType) {
40+
case telephony.PhoneTypeGsm:
4041
fmt.Println("GSM")
41-
case int32(telephony.PhoneTypeCdma):
42+
case telephony.PhoneTypeCdma:
4243
fmt.Println("CDMA")
4344
}
4445

@@ -80,8 +81,8 @@ name, err := device.GetName()
8081
addr, err := device.GetAddress()
8182
devType, err := device.GetType()
8283

83-
// Compare with named constants
84-
if devType == int32(bluetooth.DeviceTypeLe) {
84+
// Compare with named constants (cast int32 to int to match constant type)
85+
if int(devType) == bluetooth.DeviceTypeLe {
8586
fmt.Println("BLE device")
8687
}
8788
```
@@ -119,7 +120,7 @@ Use it in comparisons, switch statements, and as arguments:
119120

120121
```go
121122
// GOOD: named constant
122-
if bondState == int32(bluetooth.BondBonded) { ... }
123+
if int(bondState) == bluetooth.BondBonded { ... }
123124

124125
// BAD: magic number
125126
if bondState == 12 { ... }

0 commit comments

Comments
 (0)