From c74a915fdb4dec6490e009ba1b8d972a1396dfdf Mon Sep 17 00:00:00 2001 From: folbrich Date: Sat, 14 Feb 2026 10:59:37 +0100 Subject: [PATCH] Apply go modernize across codebase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Run 'go modernize' to apply idiomatic Go modernizations: - interface{} → any - for i := 0; i < n; i++ → for range n / for i := range n - if/else → min() builtin - []byte(fmt.Sprintf(...)) → fmt.Appendf - strings.Split + range → strings.SplitSeq - Build tag cleanup: add //go:build or remove redundant // +build - Minor comment formatting fixes --- archive.go | 6 +++--- archive_test.go | 4 ++-- assemble_test.go | 4 ++-- blocksize.go | 1 + chop.go | 2 +- chunker.go | 17 +++++++---------- cmd/desync/credentials.go | 2 +- cmd/desync/info_test.go | 4 ++-- cmd/desync/main.go | 2 +- cmd/desync/mount-index.go | 1 + cmd/desync/store.go | 4 ++-- cmd/desync/tar_test.go | 1 + cmd/desync/untar_test.go | 1 - compress.go | 1 + compress_datadog.go | 1 + copy.go | 2 +- coverter.go | 2 +- dedupqueue.go | 6 +++--- dedupqueue_test.go | 2 +- failover_test.go | 2 +- format.go | 6 +++--- format_test.go | 2 +- index.go | 2 +- ioctl_linux.go | 1 - ioctl_nonlinux.go | 1 + local.go | 2 +- localfs_other.go | 1 + make.go | 2 +- mount-index.go | 1 + mount-sparse.go | 1 + selfseed_test.go | 2 +- sequencer.go | 4 ++-- sparse-file.go | 2 +- sparse-file_test.go | 2 +- tar_test.go | 5 ++--- untar.go | 2 +- verifyindex.go | 2 +- 37 files changed, 53 insertions(+), 50 deletions(-) diff --git a/archive.go b/archive.go index 9e186a2..3b75f8c 100644 --- a/archive.go +++ b/archive.go @@ -62,7 +62,7 @@ type NodeDevice struct { type ArchiveDecoder struct { d FormatDecoder dir string - last interface{} + last any } // NewArchiveDecoder initializes a decoder for a catar archive. @@ -73,7 +73,7 @@ func NewArchiveDecoder(r io.Reader) ArchiveDecoder { // Next returns a node from an archive, or nil if the end is reached. If NodeFile // is returned, the caller should read the file body before calling Next() again // as that invalidates the reader. -func (a *ArchiveDecoder) Next() (interface{}, error) { +func (a *ArchiveDecoder) Next() (any, error) { var ( entry *FormatEntry payload *FormatPayload @@ -81,7 +81,7 @@ func (a *ArchiveDecoder) Next() (interface{}, error) { device *FormatDevice xattrs map[string]string name string - c interface{} + c any err error ) diff --git a/archive_test.go b/archive_test.go index c23a60f..93bb725 100644 --- a/archive_test.go +++ b/archive_test.go @@ -17,7 +17,7 @@ func TestArchiveDecoderTypes(t *testing.T) { d := NewArchiveDecoder(f) // Define an array of what is expected in the test file - expected := []interface{}{ + expected := []any{ NodeDirectory{}, NodeDevice{}, NodeFile{}, @@ -48,7 +48,7 @@ func TestArchiveDecoderNesting(t *testing.T) { // Define an array of what is expected in the test file expected := []struct { - Type interface{} + Type any Name string UID int GID int diff --git a/assemble_test.go b/assemble_test.go index 7d81e09..88e2cb7 100644 --- a/assemble_test.go +++ b/assemble_test.go @@ -19,7 +19,7 @@ func TestExtract(t *testing.T) { if err != nil { t.Fatal(err) } - for i := 0; i < 4; i++ { // Replicate it a few times to make sure we get dupes + for range 4 { // Replicate it a few times to make sure we get dupes b = append(b, b...) } b = append(b, make([]byte, 2*ChunkSizeMaxDefault)...) // want to have at least one null-chunk in the input @@ -299,7 +299,7 @@ func TestSelfSeedInPlace(t *testing.T) { numChunks := 10 chunks := make([]rawChunk, numChunks) - for i := 0; i < numChunks; i++ { + for i := range numChunks { b := make([]byte, size) rand.Read(b) chunk := NewChunk(b) diff --git a/blocksize.go b/blocksize.go index 81c078f..06d7ceb 100644 --- a/blocksize.go +++ b/blocksize.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package desync diff --git a/chop.go b/chop.go index 9a64175..36dd91c 100644 --- a/chop.go +++ b/chop.go @@ -23,7 +23,7 @@ func ChopFile(ctx context.Context, name string, chunks []IndexChunk, ws WriteSto s := NewChunkStorage(ws) // Start the workers, each having its own filehandle to read concurrently - for i := 0; i < n; i++ { + for range n { f, err := os.Open(name) if err != nil { return fmt.Errorf("unable to open file %s, %s", name, err) diff --git a/chunker.go b/chunker.go index 9e0bcd3..6abb0d7 100644 --- a/chunker.go +++ b/chunker.go @@ -18,12 +18,12 @@ func discriminatorFromAvg(avg uint64) uint32 { // modulo 2^32 using Newton's method. This is used for Lemire's fast // divisibility test which replaces expensive hardware division. func modInverse32(d uint32) uint32 { - x := d // d is odd, so d*d ≡ 1 (mod 4) is a valid start - x *= 2 - d*x // 3 bits - x *= 2 - d*x // 6 bits - x *= 2 - d*x // 12 bits - x *= 2 - d*x // 24 bits - x *= 2 - d*x // 48 bits → full 32-bit precision + x := d // d is odd, so d*d ≡ 1 (mod 4) is a valid start + x *= 2 - d*x // 3 bits + x *= 2 - d*x // 6 bits + x *= 2 - d*x // 12 bits + x *= 2 - d*x // 24 bits + x *= 2 - d*x // 48 bits → full 32-bit precision return x } @@ -218,10 +218,7 @@ func (c *Chunker) Next() (uint64, []byte, error) { // m is the upper boundary for the current chunk. It's either c.max if we have // enough bytes in the buffer, or len(c.buf) - m := int(c.max) - if len(c.buf) < int(c.max) { - m = len(c.buf) - } + m := min(len(c.buf), int(c.max)) // Initialize the rolling hash window with the ChunkerWindowSize bytes // immediately prior to min size diff --git a/cmd/desync/credentials.go b/cmd/desync/credentials.go index 1c6a716..acfa870 100644 --- a/cmd/desync/credentials.go +++ b/cmd/desync/credentials.go @@ -17,7 +17,7 @@ import ( // Builds the shared config file path based on the OS's platform. // // - Linux/Unix: $HOME/.aws/credentials -// - Windows %USERPROFILE%\.aws\credentials +// - Windows %USERPROFILE%\.aws\credentials func SharedCredentialsFilename() (string, error) { homeDir, err := os.UserHomeDir() if err != nil { diff --git a/cmd/desync/info_test.go b/cmd/desync/info_test.go index 0deed41..d6bddc5 100644 --- a/cmd/desync/info_test.go +++ b/cmd/desync/info_test.go @@ -103,7 +103,7 @@ func TestInfoCommand(t *testing.T) { }`)}, } { t.Run(test.name, func(t *testing.T) { - exp := make(map[string]interface{}) + exp := make(map[string]any) err := json.Unmarshal(test.expectedOutput, &exp) require.NoError(t, err) @@ -118,7 +118,7 @@ func TestInfoCommand(t *testing.T) { require.NoError(t, err) // Decode the output and compare to what's expected - got := make(map[string]interface{}) + got := make(map[string]any) err = json.Unmarshal(b.Bytes(), &got) require.NoError(t, err) require.Equal(t, exp, got) diff --git a/cmd/desync/main.go b/cmd/desync/main.go index 298e0bb..548b721 100644 --- a/cmd/desync/main.go +++ b/cmd/desync/main.go @@ -71,7 +71,7 @@ func main() { } } -func printJSON(w io.Writer, v interface{}) error { +func printJSON(w io.Writer, v any) error { b, err := json.MarshalIndent(v, "", " ") if err != nil { return err diff --git a/cmd/desync/mount-index.go b/cmd/desync/mount-index.go index ea597a8..9cc3428 100644 --- a/cmd/desync/mount-index.go +++ b/cmd/desync/mount-index.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package main diff --git a/cmd/desync/store.go b/cmd/desync/store.go index cb5b2e6..3f1817f 100644 --- a/cmd/desync/store.go +++ b/cmd/desync/store.go @@ -68,8 +68,8 @@ func storeGroup(location string, cmdOpt cmdStoreOptions) (desync.Store, error) { return storeFromLocation(location, cmdOpt) } var stores []desync.Store - members := strings.Split(location, "|") - for _, m := range members { + members := strings.SplitSeq(location, "|") + for m := range members { s, err := storeFromLocation(m, cmdOpt) if err != nil { return nil, err diff --git a/cmd/desync/tar_test.go b/cmd/desync/tar_test.go index 6daeb2b..2bc94f3 100644 --- a/cmd/desync/tar_test.go +++ b/cmd/desync/tar_test.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package main diff --git a/cmd/desync/untar_test.go b/cmd/desync/untar_test.go index a7b8662..0849ab2 100644 --- a/cmd/desync/untar_test.go +++ b/cmd/desync/untar_test.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package main diff --git a/compress.go b/compress.go index a841710..c715638 100644 --- a/compress.go +++ b/compress.go @@ -1,3 +1,4 @@ +//go:build !datadog // +build !datadog package desync diff --git a/compress_datadog.go b/compress_datadog.go index ab5db1c..2266ab6 100644 --- a/compress_datadog.go +++ b/compress_datadog.go @@ -1,3 +1,4 @@ +//go:build datadog // +build datadog package desync diff --git a/copy.go b/copy.go index 940d4c8..3bb3425 100644 --- a/copy.go +++ b/copy.go @@ -20,7 +20,7 @@ func Copy(ctx context.Context, ids []ChunkID, src Store, dst WriteStore, n int, defer pb.Finish() // Start the workers - for i := 0; i < n; i++ { + for range n { g.Go(func() error { for id := range in { pb.Increment() diff --git a/coverter.go b/coverter.go index 23d78fe..384f673 100644 --- a/coverter.go +++ b/coverter.go @@ -55,7 +55,7 @@ func (s Converters) equal(c Converters) bool { if len(s) != len(c) { return false } - for i := 0; i < len(s); i++ { + for i := range s { if !s[i].equal(c[i]) { return false } diff --git a/dedupqueue.go b/dedupqueue.go index 53b847a..5a58ab9 100644 --- a/dedupqueue.go +++ b/dedupqueue.go @@ -111,7 +111,7 @@ func (q *queue) delete(id ChunkID) { // queueRequests is used to dedup requests for GetChunk() or HasChunk() with the data // being either the chunk itself or a bool in case of HasChunk(). type request struct { - data interface{} + data any err error done chan struct{} } @@ -121,13 +121,13 @@ func newRequest() *request { } // Wait for the request to complete. Returns the data as well as the error from the request. -func (r *request) wait() (interface{}, error) { +func (r *request) wait() (any, error) { <-r.done return r.data, r.err } // Set the result data and marks this request as complete. -func (r *request) markDone(data interface{}, err error) { +func (r *request) markDone(data any, err error) { r.data = data r.err = err close(r.done) diff --git a/dedupqueue_test.go b/dedupqueue_test.go index a2b71cb..233b65e 100644 --- a/dedupqueue_test.go +++ b/dedupqueue_test.go @@ -72,7 +72,7 @@ func TestDedupQueueParallel(t *testing.T) { ) // Start several goroutines all asking for the same chunk from the store - for i := 0; i < 10; i++ { + for range 10 { wg.Add(1) go func() { <-start diff --git a/failover_test.go b/failover_test.go index e1fe5c2..b313b13 100644 --- a/failover_test.go +++ b/failover_test.go @@ -85,7 +85,7 @@ func TestFailoverMutliple(t *testing.T) { defer cancel() // Run several goroutines querying the group in a tight loop - for i := 0; i < 16; i++ { + for range 16 { eg.Go(func() error { var id ChunkID for { diff --git a/format.go b/format.go index 8296bf5..e298cfd 100644 --- a/format.go +++ b/format.go @@ -143,7 +143,7 @@ func NewFormatDecoder(r io.Reader) FormatDecoder { // Next returns the next format element from the stream. If an element // contains a reader, that reader should be used before any subsequent calls as // it'll be invalidated then. Returns nil when the end is reached. -func (d *FormatDecoder) Next() (interface{}, error) { +func (d *FormatDecoder) Next() (any, error) { // If we previously returned a reader, make sure we advance all the way in // case the caller didn't read it all. if d.advance != nil { @@ -347,7 +347,7 @@ func (d *FormatDecoder) Next() (interface{}, error) { n := (hdr.Size - 16) / 24 items := make([]FormatGoodbyeItem, n) e := FormatGoodbye{FormatHeader: hdr, Items: items} - for i := uint64(0); i < n; i++ { + for i := range n { items[i].Offset, err = d.r.ReadUint64() if err != nil { return nil, err @@ -448,7 +448,7 @@ func NewFormatEncoder(w io.Writer) FormatEncoder { return FormatEncoder{w: writer{w}} } -func (e *FormatEncoder) Encode(v interface{}) (int64, error) { +func (e *FormatEncoder) Encode(v any) (int64, error) { switch t := v.(type) { case FormatEntry: return e.w.WriteUint64( diff --git a/format_test.go b/format_test.go index 187c32d..5c7c658 100644 --- a/format_test.go +++ b/format_test.go @@ -17,7 +17,7 @@ func TestFormatDecoder(t *testing.T) { d := NewFormatDecoder(f) // Define an array of what is expected in the test file - expected := []interface{}{ + expected := []any{ FormatEntry{}, FormatUser{}, FormatGroup{}, diff --git a/index.go b/index.go index 8d9f1c3..bd201da 100644 --- a/index.go +++ b/index.go @@ -160,7 +160,7 @@ func ChunkStream(ctx context.Context, c Chunker, ws WriteStore, n int) (Index, e // Start the workers responsible for checksum calculation, compression and // storage (if required). Each job comes with a chunk number for sorting later - for i := 0; i < n; i++ { + for range n { g.Go(func() error { for c := range in { // Create a chunk object, needed to calculate the checksum diff --git a/ioctl_linux.go b/ioctl_linux.go index 62f8199..96313cb 100644 --- a/ioctl_linux.go +++ b/ioctl_linux.go @@ -1,5 +1,4 @@ //go:build linux -// +build linux package desync diff --git a/ioctl_nonlinux.go b/ioctl_nonlinux.go index b6392b3..5862898 100644 --- a/ioctl_nonlinux.go +++ b/ioctl_nonlinux.go @@ -1,3 +1,4 @@ +//go:build !linux // +build !linux package desync diff --git a/local.go b/local.go index 19aef16..1fd5c38 100644 --- a/local.go +++ b/local.go @@ -95,7 +95,7 @@ func (s LocalStore) Verify(ctx context.Context, n int, repair bool, w io.Writer) ids := make(chan ChunkID) // Start the workers - for i := 0; i < n; i++ { + for range n { wg.Add(1) go func() { for id := range ids { diff --git a/localfs_other.go b/localfs_other.go index 1199293..85b3ce7 100644 --- a/localfs_other.go +++ b/localfs_other.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package desync diff --git a/make.go b/make.go index 92fe24b..bd5a8b3 100644 --- a/make.go +++ b/make.go @@ -222,7 +222,7 @@ func (c *pChunker) start(ctx context.Context) { return } nc := chunk - for i := 0; i < numNullChunks; i++ { + for range numNullChunks { nc = IndexChunk{Start: nc.Start + nc.Size, Size: uint64(len(c.nullChunk.Data)), ID: c.nullChunk.ID} c.results <- nc zeroes -= uint64(len(c.nullChunk.Data)) diff --git a/mount-index.go b/mount-index.go index ea6eded..28dac33 100644 --- a/mount-index.go +++ b/mount-index.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package desync diff --git a/mount-sparse.go b/mount-sparse.go index dbf1209..cbd7077 100644 --- a/mount-sparse.go +++ b/mount-sparse.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package desync diff --git a/selfseed_test.go b/selfseed_test.go index 055dd9c..93c38e8 100644 --- a/selfseed_test.go +++ b/selfseed_test.go @@ -26,7 +26,7 @@ func TestSelfSeed(t *testing.T) { numChunks := 10 chunks := make([]rawChunk, numChunks) - for i := 0; i < numChunks; i++ { + for i := range numChunks { b := make([]byte, size) rand.Read(b) chunk := NewChunk(b) diff --git a/sequencer.go b/sequencer.go index 57d7a07..1248a06 100644 --- a/sequencer.go +++ b/sequencer.go @@ -78,7 +78,7 @@ func (r *SeedSequencer) Rewind() { r.current = 0 } -//isFileSeed returns true if this segment is pointing to a fileSeed +// isFileSeed returns true if this segment is pointing to a fileSeed func (s SeedSegmentCandidate) isFileSeed() bool { // We expect an empty filename when using nullSeeds return s.source != nil && s.source.FileName() != "" @@ -145,7 +145,7 @@ func (p Plan) Validate(ctx context.Context, n int, pb ProgressBar) (err error) { } g, ctx := errgroup.WithContext(ctx) // Concurrently validate all the chunks in this plan - for i := 0; i < n; i++ { + for range n { g.Go(func() error { for job := range in { if err := job.candidate.source.Validate(job.file); err != nil { diff --git a/sparse-file.go b/sparse-file.go index fe695d7..dd3bff0 100644 --- a/sparse-file.go +++ b/sparse-file.go @@ -298,7 +298,7 @@ func (l *sparseFileLoader) preloadChunksFromState(r io.Reader, n int) error { // Start the workers for parallel pre-loading ch := make(chan int) - for i := 0; i < n; i++ { + for range n { go func() { for chunkIdx := range ch { _ = l.loadChunk(chunkIdx) diff --git a/sparse-file_test.go b/sparse-file_test.go index e1bcad1..cc53a35 100644 --- a/sparse-file_test.go +++ b/sparse-file_test.go @@ -79,7 +79,7 @@ func TestSparseFileRead(t *testing.T) { defer h.Close() // Read a few random ranges and compare to the expected blob content - for i := 0; i < 1000; i++ { + for range 1000 { length := rand.Intn(int(index.Index.ChunkSizeMax)) offset := rand.Intn(int(index.Length()) - length - 1) diff --git a/tar_test.go b/tar_test.go index 066fb09..5460416 100644 --- a/tar_test.go +++ b/tar_test.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package desync @@ -34,7 +33,7 @@ func TestTar(t *testing.T) { "dir1/sub11/f12", } for i, name := range files { - os.WriteFile(filepath.Join(base, name), []byte(fmt.Sprintf("filecontent%d", i)), 0644) + os.WriteFile(filepath.Join(base, name), fmt.Appendf(nil, "filecontent%d", i), 0644) } if err := os.Symlink("dir1", filepath.Join(base, "symlink")); err != nil { @@ -52,7 +51,7 @@ func TestTar(t *testing.T) { d := NewFormatDecoder(b) // Define an array of what is expected in the test file - expected := []interface{}{ + expected := []any{ FormatEntry{}, FormatFilename{}, // "dir1" FormatEntry{}, diff --git a/untar.go b/untar.go index d0817f4..a677b57 100644 --- a/untar.go +++ b/untar.go @@ -71,7 +71,7 @@ func UnTarIndex(ctx context.Context, fs FilesystemWriter, index Index, s Store, r, w := io.Pipe() // Workers - getting chunks from the store - for i := 0; i < n; i++ { + for range n { g.Go(func() error { for r := range req { // Pull the chunk from the store diff --git a/verifyindex.go b/verifyindex.go index 2cd7ee4..b1179ba 100644 --- a/verifyindex.go +++ b/verifyindex.go @@ -28,7 +28,7 @@ func VerifyIndex(ctx context.Context, name string, idx Index, n int, pb Progress } // Start the workers, each having its own filehandle to read concurrently - for i := 0; i < n; i++ { + for range n { f, err := os.Open(name) if err != nil { return fmt.Errorf("unable to open file %s, %s", name, err)