Skip to content

refactor: modernize Go syntax with range over integers#17

Open
AWaterColorPen wants to merge 3 commits intomainfrom
feature/go-modernization-syntax
Open

refactor: modernize Go syntax with range over integers#17
AWaterColorPen wants to merge 3 commits intomainfrom
feature/go-modernization-syntax

Conversation

@AWaterColorPen
Copy link
Copy Markdown
Owner

@AWaterColorPen AWaterColorPen commented Mar 27, 2026

Description

This PR modernizes Go syntax by replacing traditional for loops with Go 1.22+ range over integers syntax, where safe to do so.

Changes

  • Updated dictionary_column.go and dictionary_splitter.go to use for i := range len(slice) syntax
  • Kept classic for i := 0; i < len(queue); i++ form for BFS/topological-sort loops that dynamically grow their queue inside the loop body

Bug Fix (added in follow-up commit)

The original commit incorrectly applied for i := range len(queue) to three dynamic-queue loops:

  • api/models/graph.goGetTree (BFS, queue grows in loop)
  • dependency_graph.gosort (topological sort, queue grows in loop)
  • dictionary_adapter.goGetDependencyTree (BFS, queue grows in loop)

range len(queue) evaluates len(queue) once at entry, so it cannot traverse elements appended during iteration. These have been reverted to the classic form.

Why range-len is safe for the other two

  • dictionary_column.go — iterates a fixed-length sorted slice, no mutation
  • dictionary_splitter.go — iterates dl1 which is not modified in the loop body

Testing

  • All existing tests pass ✅
  • Changes are purely syntactic for the safe cases, no functional changes

Related to Phase 1 of olap-sql modernization plan

This is part of the Phase 1 task: "使用新 Go 语法重构" (Use new Go syntax refactoring).

- Replace traditional for loops with Go 1.22+ range over integers syntax
- Updated files: api/models/graph.go, dictionary_splitter.go, dependency_graph.go, dictionary_adapter.go, dictionary_column.go
- Improves code readability and aligns with modern Go best practices
…S bug

The BFS/topological-sort loops in:
- api/models/graph.go (GetTree)
- dependency_graph.go (sort)
- dictionary_adapter.go (GetDependencyTree)

grow their queues inside the loop body, so 'for i := range len(queue)'
is incorrect (range evaluates len once at entry). Restore these to
'for i := 0; i < len(queue); i++' which re-evaluates len each iteration.

Static-length loops in dictionary_column.go and dictionary_splitter.go
are safe to keep as 'for i := range len(...)'.

All tests pass.
The Go modernization syntax PR uses 'for i := range len(...)' (Go 1.22+),
but the CI workflows still specified go-version: 1.18, causing build failures.
Update both go.yml and clickhouse.yml to use Go 1.24 to match go.mod.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant