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