๐Ÿ”€

Control Flow โ€” if/for Look Similar but There's No while

A for-loop World Where You'll Miss Ruby's each/map/select

In Ruby, you loop through arrays with [1,2,3].each { |n| puts n }. Block-passing is second nature.

In Go: for _, n := range []int{1,2,3} { fmt.Println(n) }. No blocks, no iterators. for loops are everything.

No while

Ruby's while condition do ... end becomes for condition { ... } in Go. The keyword is for but it acts like while. Go unified all loops into for.

Infinite loops: for { ... } โ€” Ruby's loop do ... end.

No .map, .select

What Ruby does in numbers.map { |n| n * 2 } takes Go three lines:

result := make([]int, len(numbers))
for i, n := range numbers {
    result[i] = n * 2
}

Go community calls it "explicit." Ruby developers call it "verbose." Both are correct.

No Parentheses in if

Ruby doesn't use parentheses in if conditions. Neither does Go. Similar. But Go's if can include a short statement: if err := doSomething(); err != nil { ... } โ€” a pattern Ruby doesn't have.

switch Is Powerful

Ruby's case/when supports pattern matching. Go's switch is similar but fallthrough is off by default (opposite of C). There's also a type switch โ€” switch v := x.(type) { case int: ... } is similar to Ruby's case x when Integer.

Ruby to Go

1

Ruby: while/until/loop โ†’ Go: for handles everything (no while)

2

Ruby: .each { |x| } block pattern โ†’ Go: for _, x := range pattern

3

Ruby: .map/.select method chaining โ†’ Go: write for loops manually (no built-in)

4

Ruby: case/when โ†’ Go: switch (fallthrough off by default, type switch supported)

Pros

  • Unified into for โ€” less to learn. No while, until, loop variants
  • if can include short statements โ€” err handling stays clean within scope

Cons

  • No .map/.select/.reduce means collection processing code is 3-5x longer
  • No block/Proc/Lambda first-class function patterns โ€” less expressive

Use Cases

When transitioning from Ruby's Enumerable method chaining to Go style When implementing Ruby's .select.map pattern with for range + if