build: modernize Go usage

This commit modernizes Go usage. This was done with:

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

Then files needed to be `go fmt`ed and a few comments needed to be
restored.

The modernizations include replacing

- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
This commit is contained in:
Nick Craig-Wood
2025-02-28 11:31:14 +00:00
parent 431386085f
commit 401cf81034
206 changed files with 755 additions and 953 deletions
+7 -7
View File
@@ -593,7 +593,7 @@ func DeleteFilesWithBackupDir(ctx context.Context, toBeDeleted fs.ObjectsChan, b
var errorCount atomic.Int32
var fatalErrorCount atomic.Int32
for i := 0; i < ci.Checkers; i++ {
for range ci.Checkers {
go func() {
defer wg.Done()
for dst := range toBeDeleted {
@@ -732,7 +732,7 @@ func SameDir(fdst, fsrc fs.Info) bool {
}
// Retry runs fn up to maxTries times if it returns a retriable error
func Retry(ctx context.Context, o interface{}, maxTries int, fn func() error) (err error) {
func Retry(ctx context.Context, o any, maxTries int, fn func() error) (err error) {
for tries := 1; tries <= maxTries; tries++ {
// Call the function which might error
err = fn()
@@ -777,7 +777,7 @@ var StdoutMutex sync.Mutex
// This writes to stdout holding the StdoutMutex. If you are going to
// override it and write to os.Stdout then you should hold the
// StdoutMutex too.
var SyncPrintf = func(format string, a ...interface{}) {
var SyncPrintf = func(format string, a ...any) {
StdoutMutex.Lock()
defer StdoutMutex.Unlock()
fmt.Printf(format, a...)
@@ -788,7 +788,7 @@ var SyncPrintf = func(format string, a ...interface{}) {
// Ignores errors from Fprintf.
//
// Prints to stdout if w is nil
func SyncFprintf(w io.Writer, format string, a ...interface{}) {
func SyncFprintf(w io.Writer, format string, a ...any) {
if w == nil || w == os.Stdout {
SyncPrintf(format, a...)
} else {
@@ -2443,7 +2443,7 @@ func DirMove(ctx context.Context, f fs.Fs, srcRemote, dstRemote string) (err err
}
renames := make(chan rename, ci.Checkers)
g, gCtx := errgroup.WithContext(context.Background())
for i := 0; i < ci.Checkers; i++ {
for range ci.Checkers {
g.Go(func() error {
for job := range renames {
dstOverwritten, _ := f.NewObject(gCtx, job.newPath)
@@ -2551,7 +2551,7 @@ var (
// skipDestructiveChoose asks the user which action to take
//
// Call with interactiveMu held
func skipDestructiveChoose(ctx context.Context, subject interface{}, action string) (skip bool) {
func skipDestructiveChoose(ctx context.Context, subject any, action string) (skip bool) {
// Lock the StdoutMutex - must not call fs.Log anything
// otherwise it will deadlock with --interactive --progress
StdoutMutex.Lock()
@@ -2601,7 +2601,7 @@ func skipDestructiveChoose(ctx context.Context, subject interface{}, action stri
//
// Together they should make sense in this sentence: "Rclone is about
// to action subject".
func SkipDestructive(ctx context.Context, subject interface{}, action string) (skip bool) {
func SkipDestructive(ctx context.Context, subject any, action string) (skip bool) {
var flag string
ci := fs.GetConfig(ctx)
switch {