-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.go
More file actions
38 lines (33 loc) · 862 Bytes
/
misc.go
File metadata and controls
38 lines (33 loc) · 862 Bytes
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
package main
import (
"log"
)
// Iterates over a slice with a redundant nil check.
func SliceWithNilCheck(slice []string) {
// nolint:gosimple // for demonstration purposes
if slice != nil {
for _, s := range slice {
log.Println(s)
}
}
}
// Iterates over a slice without a nil check. Unlike other
// languages - such as Java - Go does not require a nil
// check when iterating over an empty or nil slice.
func SliceNoNilCheck(slice []string) {
for _, s := range slice {
log.Println(s)
}
}
// Swaps values of the provided pointers using
// an unnecessary temporary variable.
func SwapValuesWithTemp(a *string, b *string) {
// nolint:gocritic // for demonstration purposes
temp := *a
*a = *b
*b = temp
}
// Swaps values of the provided pointers without a temporary variable.
func SwapValuesNoTemp(a *string, b *string) {
*a, *b = *b, *a
}