Skip to content

Commit aca3b32

Browse files
authored
refactor: use modern Go constructs (canonical#215)
Use modern Go constructs: - Replace `interface{}` with `any` (available since Go 1.18) - Replace 3-clause loop with `range` loop (available since Go 1.22) - Use `slices` package (available since Go 1.21)
1 parent 680d53c commit aca3b32

29 files changed

Lines changed: 68 additions & 72 deletions

File tree

‎cmd/chisel/log.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetDebug(debug bool) {
3333

3434
// logf sends to the logger registered via SetLogger the string resulting
3535
// from running format and args through Sprintf.
36-
func logf(format string, args ...interface{}) {
36+
func logf(format string, args ...any) {
3737
globalLoggerLock.Lock()
3838
defer globalLoggerLock.Unlock()
3939
if globalLogger != nil {
@@ -44,7 +44,7 @@ func logf(format string, args ...interface{}) {
4444
// debugf sends to the logger registered via SetLogger the string resulting
4545
// from running format and args through Sprintf, but only if debugging was
4646
// enabled via SetDebug.
47-
func debugf(format string, args ...interface{}) {
47+
func debugf(format string, args ...any) {
4848
globalLoggerLock.Lock()
4949
defer globalLoggerLock.Unlock()
5050
if globalDebug && globalLogger != nil {
@@ -55,7 +55,7 @@ func debugf(format string, args ...interface{}) {
5555
// panicf sends to the logger registered via SetLogger the string resulting
5656
// from running format and args through Sprintf, and then panics with the
5757
// same message.
58-
func panicf(format string, args ...interface{}) {
58+
func panicf(format string, args ...any) {
5959
globalLoggerLock.Lock()
6060
defer globalLoggerLock.Unlock()
6161
if globalDebug && globalLogger != nil {

‎internal/apacheutil/log.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func SetDebug(debug bool) {
3535

3636
// logf sends to the logger registered via SetLogger the string resulting
3737
// from running format and args through Sprintf.
38-
func logf(format string, args ...interface{}) {
38+
func logf(format string, args ...any) {
3939
globalLoggerLock.Lock()
4040
defer globalLoggerLock.Unlock()
4141
if globalLogger != nil {
@@ -46,7 +46,7 @@ func logf(format string, args ...interface{}) {
4646
// debugf sends to the logger registered via SetLogger the string resulting
4747
// from running format and args through Sprintf, but only if debugging was
4848
// enabled via SetDebug.
49-
func debugf(format string, args ...interface{}) {
49+
func debugf(format string, args ...any) {
5050
globalLoggerLock.Lock()
5151
defer globalLoggerLock.Unlock()
5252
if globalDebug && globalLogger != nil {

‎internal/archive/archive.go‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -348,16 +349,10 @@ func (index *ubuntuIndex) supportsArch(arch string) bool {
348349

349350
func (index *ubuntuIndex) checkComponents(components []string) error {
350351
releaseComponents := strings.Fields(index.release.Get("Components"))
351-
for _, c1 := range components {
352-
found := false
353-
for _, c2 := range releaseComponents {
354-
if c1 == c2 {
355-
found = true
356-
break
357-
}
358-
}
352+
for _, c := range components {
353+
found := slices.Contains(releaseComponents, c)
359354
if !found {
360-
return fmt.Errorf("archive has no component %q", c1)
355+
return fmt.Errorf("archive has no component %q", c)
361356
}
362357
}
363358
return nil

‎internal/archive/archive_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
type httpSuite struct {
26-
logf func(string, ...interface{})
26+
logf func(string, ...any)
2727
base string
2828
request *http.Request
2929
requests []*http.Request
@@ -119,7 +119,7 @@ func (s *httpSuite) prepareArchiveAdjustRelease(suite, version, arch string, com
119119
Component: component,
120120
Arch: arch,
121121
}
122-
for j := 0; j < 2; j++ {
122+
for j := range 2 {
123123
seq := 1 + i*2 + j
124124
index.Packages = append(index.Packages, &testarchive.Package{
125125
Name: fmt.Sprintf("mypkg%d", seq),

‎internal/archive/log.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetDebug(debug bool) {
3333

3434
// logf sends to the logger registered via SetLogger the string resulting
3535
// from running format and args through Sprintf.
36-
func logf(format string, args ...interface{}) {
36+
func logf(format string, args ...any) {
3737
globalLoggerLock.Lock()
3838
defer globalLoggerLock.Unlock()
3939
if globalLogger != nil {
@@ -44,7 +44,7 @@ func logf(format string, args ...interface{}) {
4444
// debugf sends to the logger registered via SetLogger the string resulting
4545
// from running format and args through Sprintf, but only if debugging was
4646
// enabled via SetDebug.
47-
func debugf(format string, args ...interface{}) {
47+
func debugf(format string, args ...any) {
4848
globalLoggerLock.Lock()
4949
defer globalLoggerLock.Unlock()
5050
if globalDebug && globalLogger != nil {

‎internal/deb/chrorder/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func main() {
5959
fmt.Fprintf(out, "package %v\n", pkgName)
6060
fmt.Fprintf(out, "\n")
6161
fmt.Fprintln(out, "var chOrder = [...]int{")
62-
for i := 0; i < 16; i++ {
62+
for i := range 16 {
6363
fmt.Fprintf(out, "\t")
64-
for j := 0; j < 16; j++ {
64+
for j := range 16 {
6565
if j != 0 {
6666
fmt.Fprintf(out, " ")
6767
}

‎internal/deb/log.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetDebug(debug bool) {
3333

3434
// logf sends to the logger registered via SetLogger the string resulting
3535
// from running format and args through Sprintf.
36-
func logf(format string, args ...interface{}) {
36+
func logf(format string, args ...any) {
3737
globalLoggerLock.Lock()
3838
defer globalLoggerLock.Unlock()
3939
if globalLogger != nil {
@@ -44,7 +44,7 @@ func logf(format string, args ...interface{}) {
4444
// debugf sends to the logger registered via SetLogger the string resulting
4545
// from running format and args through Sprintf, but only if debugging was
4646
// enabled via SetDebug.
47-
func debugf(format string, args ...interface{}) {
47+
func debugf(format string, args ...any) {
4848
globalLoggerLock.Lock()
4949
defer globalLoggerLock.Unlock()
5050
if globalDebug && globalLogger != nil {

‎internal/deb/version.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func max(a, b int) int {
3333
//go:generate go run ./chrorder/main.go -package=deb -output=chrorder.go
3434

3535
func cmpString(as, bs string) int {
36-
for i := 0; i < max(len(as), len(bs)); i++ {
36+
for i := range max(len(as), len(bs)) {
3737
var a uint8
3838
var b uint8
3939
if i < len(as) {
@@ -53,7 +53,7 @@ func cmpString(as, bs string) int {
5353
}
5454

5555
func trimLeadingZeroes(a string) string {
56-
for i := 0; i < len(a); i++ {
56+
for i := range len(a) {
5757
if a[i] != '0' {
5858
return a[i:]
5959
}
@@ -72,7 +72,7 @@ func cmpNumeric(a, b string) int {
7272
case d < 0:
7373
return -1
7474
}
75-
for i := 0; i < len(a); i++ {
75+
for i := range len(a) {
7676
switch {
7777
case a[i] > b[i]:
7878
return 1

‎internal/fsutil/log.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetDebug(debug bool) {
3333

3434
// logf sends to the logger registered via SetLogger the string resulting
3535
// from running format and args through Sprintf.
36-
func logf(format string, args ...interface{}) {
36+
func logf(format string, args ...any) {
3737
globalLoggerLock.Lock()
3838
defer globalLoggerLock.Unlock()
3939
if globalLogger != nil {
@@ -44,7 +44,7 @@ func logf(format string, args ...interface{}) {
4444
// debugf sends to the logger registered via SetLogger the string resulting
4545
// from running format and args through Sprintf, but only if debugging was
4646
// enabled via SetDebug.
47-
func debugf(format string, args ...interface{}) {
47+
func debugf(format string, args ...any) {
4848
globalLoggerLock.Lock()
4949
defer globalLoggerLock.Unlock()
5050
if globalDebug && globalLogger != nil {

‎internal/manifestutil/log.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetDebug(debug bool) {
3333

3434
// logf sends to the logger registered via SetLogger the string resulting
3535
// from running format and args through Sprintf.
36-
func logf(format string, args ...interface{}) {
36+
func logf(format string, args ...any) {
3737
globalLoggerLock.Lock()
3838
defer globalLoggerLock.Unlock()
3939
if globalLogger != nil {
@@ -44,7 +44,7 @@ func logf(format string, args ...interface{}) {
4444
// debugf sends to the logger registered via SetLogger the string resulting
4545
// from running format and args through Sprintf, but only if debugging was
4646
// enabled via SetDebug.
47-
func debugf(format string, args ...interface{}) {
47+
func debugf(format string, args ...any) {
4848
globalLoggerLock.Lock()
4949
defer globalLoggerLock.Unlock()
5050
if globalDebug && globalLogger != nil {

0 commit comments

Comments
 (0)