Skip to content

Commit a261c65

Browse files
authored
Upgrade Go, use generics instead of interface, use slice.Equal and int range (#137)
1 parent cb47c27 commit a261c65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+149
-159
lines changed

.github/linters/.golangci.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ issues:
1717
exclude-rules:
1818
- path: /
1919
linters:
20-
- nosnakecase
2120
- nlreturn
22-
- forcetypeassert
21+
- gci
22+
- gomnd
2323
- ifshort
24-
- scopelint
24+
- forcetypeassert
2525
- varnamelen
2626
- lll
2727
- testpackage
28-
- gomnd
2928
- wsl
3029
- gochecknoglobals
3130
- paralleltest
@@ -34,9 +33,6 @@ issues:
3433
- dupword
3534
linters:
3635
enable-all: true
37-
enable:
38-
- revive
39-
- misspell
4036

4137
linters-settings:
4238
forbidigo:

.github/workflows/tests.yaml

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
lint:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- uses: reviewdog/action-misspell@v1
1515
with:
1616
github_token: ${{ secrets.github_token }}
1717
locale: "US"
18-
- uses: github/super-linter@v4
18+
- uses: github/super-linter@v5
1919
env:
2020
DEFAULT_BRANCH: main
2121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -30,15 +30,8 @@ jobs:
3030
tests:
3131
runs-on: ubuntu-latest
3232
steps:
33-
- uses: actions/checkout@v3
34-
- uses: actions/setup-go@v4
35-
- uses: actions/cache@v3
36-
with:
37-
path: |
38-
~/.cache/go-build
39-
~/go/pkg/
40-
~/go/bin/
41-
key: go-cache-${{ hashFiles('go.mod') }}
33+
- uses: actions/checkout@v4
34+
- uses: actions/setup-go@v5
4235
- run: go test -v -coverprofile=profile.cov ./...
4336
- uses: shogo82148/actions-goveralls@v1
4437
with:

array/add_two_numbers.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ func AddTwoNumbers(num1, num2 []int) []int {
2525

2626
func equalizeLengths(num1, num2 []int) ([]int, []int) {
2727
diff := int(math.Abs(float64(len(num2) - len(num1))))
28-
zeros := []int{}
29-
for i := 0; i < diff; i++ {
30-
zeros = append(zeros, 0)
31-
}
28+
zeros := make([]int, diff)
3229
if len(num2) > len(num1) {
3330
num1 = append(zeros, num1...)
3431
} else if len(num1) > len(num2) {

array/add_two_numbers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -26,7 +26,7 @@ func TestAddTwoNumbers(t *testing.T) {
2626
{[]int{9, 9, 9}, []int{9, 9, 9}, []int{1, 9, 9, 8}},
2727
}
2828
for i, test := range tests {
29-
if got := AddTwoNumbers(test.num1, test.num2); !reflect.DeepEqual(got, test.sum) {
29+
if got := AddTwoNumbers(test.num1, test.num2); !slices.Equal(got, test.sum) {
3030
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sum, got)
3131
}
3232
}

array/bubble_sort_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -27,7 +27,7 @@ func TestBubbleSort(t *testing.T) {
2727
}
2828
for i, test := range tests {
2929
BubbleSort(test.input)
30-
if !reflect.DeepEqual(test.input, test.sorted) {
30+
if !slices.Equal(test.input, test.sorted) {
3131
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
3232
}
3333
}

array/equal_sum_subarrays.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func EqualSubArrays(list []int) [][]int {
2020

2121
func findSplitPoint(list []int) int {
2222
lSum := 0
23-
for i := 0; i < len(list); i++ {
23+
for i := range len(list) {
2424
lSum += list[i]
2525

2626
rSum := 0

array/insertion_sort_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -28,7 +28,7 @@ func TestInsertionSort(t *testing.T) {
2828
}
2929
for i, test := range tests {
3030
InsertionSort(test.input)
31-
if !reflect.DeepEqual(test.input, test.sorted) {
31+
if !slices.Equal(test.input, test.sorted) {
3232
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
3333
}
3434
}

array/product_of_all_other_elements_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -26,7 +26,7 @@ func TestProductOfAllOtherElements(t *testing.T) {
2626
}
2727

2828
for i, test := range tests {
29-
if got := ProductOfAllOtherElements(test.list); !reflect.DeepEqual(got, test.products) {
29+
if got := ProductOfAllOtherElements(test.list); !slices.Equal(got, test.products) {
3030
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.products, got)
3131
}
3232
}

array/reverse_inplace_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -31,7 +31,7 @@ func TestReverseInPlace(t *testing.T) {
3131

3232
for i, test := range tests {
3333
ReverseInPlace(test.list, test.start, test.end)
34-
if !reflect.DeepEqual(test.list, test.reversed) {
34+
if !slices.Equal(test.list, test.reversed) {
3535
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, test.list)
3636
}
3737
}

array/rotate_k_steps_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package array
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -27,7 +27,7 @@ func TestRotateKSteps(t *testing.T) {
2727

2828
for i, test := range tests {
2929
RotateKSteps(test.list, test.steps)
30-
if !reflect.DeepEqual(test.list, test.rotatedList) {
30+
if !slices.Equal(test.list, test.rotatedList) {
3131
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.rotatedList, test.list)
3232
}
3333
}

backtracking/generate_parentheses_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package backtracking
22

33
import (
4-
"reflect"
4+
"slices"
55
"sort"
66
"testing"
77
)
@@ -32,7 +32,7 @@ func TestGenerateParentheses(t *testing.T) {
3232
if len(got) > 0 {
3333
sort.Strings(got)
3434
}
35-
if !reflect.DeepEqual(test.validParentheses, got) {
35+
if !slices.Equal(test.validParentheses, got) {
3636
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.validParentheses, got)
3737
}
3838
}

backtracking/maze.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func mazeRecursive(x, y int, path string, wallMap map[[2]int]bool, visited [][]b
2626
}
2727

2828
visited[x][y] = true
29-
for i := 0; i < 4; i++ {
29+
for i := range 4 {
3030
nx, ny := x+directions[i][0], y+directions[i][1]
3131
if nx >= 0 && nx < len(visited) && ny >= 0 && ny < len(visited[0]) && !visited[nx][ny] && !wallMap[[2]int{nx, ny}] {
3232
if result := mazeRecursive(nx, ny, path+directionLetter[i], wallMap, visited, finish); result != "" {

backtracking/maze_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package backtracking
22

33
import (
4-
"reflect"
54
"testing"
65
)
76

@@ -39,7 +38,7 @@ func TestMaze(t *testing.T) {
3938
}
4039

4140
for i, test := range tests {
42-
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); !reflect.DeepEqual(test.moves, got) {
41+
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); test.moves != got {
4342
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.moves, got)
4443
}
4544
}

backtracking/n_queens.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func nQueensRecursive(row, n int, cols []int, output Chessboard) Chessboard {
2222
output = append(output, append([]int{}, cols...))
2323
return output
2424
}
25-
for col := 0; col < n; col++ {
25+
for col := range n {
2626
if isValidQueenPlacement(row, col, cols) {
2727
cols[row] = col
2828
output = nQueensRecursive(row+1, n, cols, output)
@@ -32,7 +32,7 @@ func nQueensRecursive(row, n int, cols []int, output Chessboard) Chessboard {
3232
}
3333

3434
func isValidQueenPlacement(row, col int, cols []int) bool {
35-
for i := 0; i < row; i++ {
35+
for i := range row {
3636
if col == cols[i] || col-row == cols[i]-i || col+row == cols[i]+i {
3737
return false
3838
}

backtracking/phone_letter_combinations_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package backtracking
22

33
import (
4-
"reflect"
4+
"slices"
55
"sort"
66
"testing"
77
)
@@ -33,7 +33,7 @@ func TestPhoneLetterCombinations(t *testing.T) {
3333
if len(got) > 0 {
3434
sort.Strings(got)
3535
}
36-
if !reflect.DeepEqual(test.combinations, got) {
36+
if !slices.Equal(test.combinations, got) {
3737
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.combinations, got)
3838
}
3939
}

backtracking/sudoku.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package backtracking
33
// Sudoku solves the problem in O(9^(n*n)) time and O(n*n) space.
44
func Sudoku(board [][]int) bool {
55
for i := 0; i < len(board); i++ {
6-
for j := 0; j < len(board[0]); j++ {
6+
for j := range len(board[0]) {
77
if board[i][j] != 0 {
88
continue
99
}
@@ -24,7 +24,7 @@ func Sudoku(board [][]int) bool {
2424
}
2525

2626
func isValidSudokuPlacement(board [][]int, row, col, value int) bool {
27-
for i := 0; i < 9; i++ {
27+
for i := range 9 {
2828
if board[i][col] == value || board[row][i] == value || board[3*(row/3)+i/3][3*(col/3)+i%3] == value {
2929
return false
3030
}

dnc/merge_sort_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dnc
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -27,7 +27,7 @@ func TestMergeSort(t *testing.T) {
2727
}
2828

2929
for i, test := range tests {
30-
if got := MergeSort(test.list); !reflect.DeepEqual(got, test.sorted) {
30+
if got := MergeSort(test.list); !slices.Equal(got, test.sorted) {
3131
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
3232
}
3333
}

dnc/quick_sort.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func QuickSort(list []int) []int {
99
pivot := list[len(list)/2]
1010

1111
var less, equal, greater []int
12-
for i := 0; i < len(list); i++ {
12+
for i := range len(list) {
1313
if list[i] == pivot {
1414
equal = append(equal, list[i])
1515
}

dnc/quick_sort_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dnc
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -27,7 +27,7 @@ func TestQuickSort(t *testing.T) {
2727
}
2828

2929
for i, test := range tests {
30-
if got := QuickSort(test.list); !reflect.DeepEqual(got, test.sorted) {
30+
if got := QuickSort(test.list); !slices.Equal(got, test.sorted) {
3131
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
3232
}
3333
}

dnc/rate_limit_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dnc
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
"time"
77
)
@@ -35,15 +35,15 @@ func TestRateLimiter(t *testing.T) {
3535
for i, test := range tests {
3636
rateLimitEvents = make([]int64, 0)
3737
got := make([]bool, 0)
38-
for i := 0; i < test.firstCallTimes; i++ {
38+
for range test.firstCallTimes {
3939
got = append(got, IsAllowed(test.limitPerSecond))
4040
}
4141
time.Sleep(time.Duration(test.sleep) * time.Second)
42-
for i := 0; i < test.secondCallTimes; i++ {
42+
for range test.secondCallTimes {
4343
got = append(got, IsAllowed(test.limitPerSecond))
4444
}
4545

46-
if !reflect.DeepEqual(got, test.want) {
46+
if !slices.Equal(got, test.want) {
4747
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.want, got)
4848
}
4949
}

dnc/square_root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func SquareRoot(number, precision int) float64 {
2323
}
2424

2525
increment := 0.1
26-
for i := 0; i < precision; i++ {
26+
for range precision {
2727
for ans*ans <= float64(number) {
2828
ans += increment
2929
}

dnc/towers_of_hanoi_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dnc
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -29,7 +29,7 @@ func TestTowerOfHanoi(t *testing.T) {
2929
}
3030

3131
for i, test := range tests {
32-
if got := TowerOfHanoi(test.n, test.start, test.end); !reflect.DeepEqual(got, test.moves) {
32+
if got := TowerOfHanoi(test.n, test.start, test.end); !slices.Equal(got, test.moves) {
3333
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.moves, got)
3434
}
3535
}

0 commit comments

Comments
 (0)