| 1 |
package test |
| 2 |
|
| 3 |
// In this test suite, (1) option is always preferred. |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
//= unit import: omit parenthesis in a single-package import |
| 12 |
|
| 13 |
var ( |
| 14 |
_ = fmt.Printf |
| 15 |
_ = errors.New |
| 16 |
_ = strconv.Atoi |
| 17 |
) |
| 18 |
|
| 19 |
// T is an example type. |
| 20 |
type T struct { |
| 21 |
integer int |
| 22 |
} |
| 23 |
|
| 24 |
func zeroValPtrAlloc() { |
| 25 |
_ = new(T) |
| 26 |
_ = new(map[string]bool) |
| 27 |
_ = new([]int) |
| 28 |
//= zero value ptr alloc: use new(T) for *T allocation |
| 29 |
_ = &T{} |
| 30 |
//= zero value ptr alloc: use new(T) for *T allocation |
| 31 |
_ = &[]int{} |
| 32 |
} |
| 33 |
|
| 34 |
func emptySlice() { |
| 35 |
_ = make([]int, 0) |
| 36 |
_ = make([]float64, 0) |
| 37 |
//= empty slice: use make([]T, 0) |
| 38 |
_ = []string{} |
| 39 |
} |
| 40 |
|
| 41 |
func emptyMap() { |
| 42 |
_ = make(map[T]T) |
| 43 |
_ = make(map[*T]*T, 0) |
| 44 |
//= empty map: use make(map[K]V) |
| 45 |
_ = map[int]int{} |
| 46 |
} |
| 47 |
|
| 48 |
func hexLit() { |
| 49 |
_ = 0xff |
| 50 |
_ = 0xabcdef |
| 51 |
//= hex lit: use a-f (lower case) digits |
| 52 |
_ = 0xABCD |
| 53 |
} |
| 54 |
|
| 55 |
func rangeCheck(x, low, high int) { |
| 56 |
_ = x > low && x <= high |
| 57 |
_ = x+1 >= low && x+1 < high |
| 58 |
_ = x >= low && x <= high |
| 59 |
//= range check: use align-left, like in `x >= low && x <= high` |
| 60 |
_ = low < x || x < high |
| 61 |
} |
| 62 |
|
| 63 |
func andNot(x, y int) { |
| 64 |
_ = x &^ y |
| 65 |
_ = 123 &^ x |
| 66 |
//= and-not: remove a space between & and ^, like in `x &^ y` |
| 67 |
_ = (x + 100) & ^(y + 2) |
| 68 |
} |
| 69 |
|
| 70 |
func floatLit() { |
| 71 |
_ = 0.0 |
| 72 |
_ = 0.123 |
| 73 |
_ = 1.0 |
| 74 |
//= float lit: use explicit int/frac part, like in `1.0` and `0.1` |
| 75 |
_ = 0. |
| 76 |
//= float lit: use explicit int/frac part, like in `1.0` and `0.1` |
| 77 |
_ = .0 |
| 78 |
} |
| 79 |
|
| 80 |
func labelCase() { |
| 81 |
ALL_UPPER: |
| 82 |
FOO: |
| 83 |
//= label case: use ALL_UPPER |
| 84 |
UpperCamelCase: |
| 85 |
//= label case: use ALL_UPPER |
| 86 |
lowerCamelCase: |
| 87 |
goto ALL_UPPER |
| 88 |
goto FOO |
| 89 |
goto UpperCamelCase |
| 90 |
goto lowerCamelCase |
| 91 |
} |
| 92 |
|
| 93 |
func untypedConstCoerce() { |
| 94 |
const zero = 0 |
| 95 |
|
| 96 |
var _ int = zero |
| 97 |
var _ int32 = 10 |
| 98 |
//= untyped const coerce: specify type in LHS, like in `var x T = const` |
| 99 |
var _ = int64(zero + 1) |
| 100 |
} |
| 101 |
|
| 102 |
func threeArgs(a, b, c int) {} |
| 103 |
|
| 104 |
func argListParens() { |
| 105 |
threeArgs( |
| 106 |
1, |
| 107 |
2, |
| 108 |
3) |
| 109 |
threeArgs(1, |
| 110 |
2, |
| 111 |
3) |
| 112 |
//= arg list parens: align `)` to a same line with last argument |
| 113 |
threeArgs( |
| 114 |
1, |
| 115 |
2, |
| 116 |
3, |
| 117 |
) |
| 118 |
} |
| 119 |
|
| 120 |
func nonZeroLenTestChecker() { |
| 121 |
var ( |
| 122 |
s string |
| 123 |
b []byte |
| 124 |
m map[int]int |
| 125 |
ch chan int |
| 126 |
) |
| 127 |
|
| 128 |
// Strings are ignored. |
| 129 |
_ = len(s) >= 1 |
| 130 |
_ = len(s) >= 1 |
| 131 |
_ = len(s) >= 1 |
| 132 |
|
| 133 |
_ = len(b) != 0 |
| 134 |
_ = len(m) != 0 |
| 135 |
//= non-zero length test: use `len(s) != 0` |
| 136 |
_ = len(ch) > 0 |
| 137 |
//= non-zero length test: use `len(s) != 0` |
| 138 |
_ = len(ch) >= 1 |
| 139 |
} |
| 140 |
|
| 141 |
func defaultCaseOrder(x int, v interface{}) { |
| 142 |
switch x { |
| 143 |
default: |
| 144 |
case 10: |
| 145 |
} |
| 146 |
|
| 147 |
switch v.(type) { |
| 148 |
default: |
| 149 |
case int: |
| 150 |
case string: |
| 151 |
} |
| 152 |
|
| 153 |
//= default case order: default case should be the first case |
| 154 |
switch { |
| 155 |
case x > 20: |
| 156 |
default: |
| 157 |
} |
| 158 |
} |