๐Ÿ“ฆ

Variables & Types โ€” When You Miss Ruby's Dynamic Typing

Surviving a World Without Duck Typing

Have you ever worried about types when creating a variable in Ruby? No. name = "hello" and it's a String. Change it to name = 42 and nobody complains. The runtime handles it.

Go is different. var name string = "hello" โ€” you declare the type. Short form is name := "hello", but even then Go's compiler infers and locks the type. Once a string, always a string. name = 42 gives a compile error.

How := Differs from Ruby's =

Ruby's = lets you reassign any value. Go's := declares and initializes in one step, but the type can never change afterward. x = []; x = {} works in Ruby โ€” impossible in Go.

Zero Values โ€” Instead of nil

In Ruby, uninitialized variables are nil. In Go, each type has a defined zero value. int is 0, string is "", bool is false, pointers are nil. Ruby's single nil for everything looks simpler in comparison.

Type Conversion Isn't Automatic

Ruby: "count: " + 42.to_s. Go: "count: " + strconv.Itoa(42) โ€” explicit conversion required. Even int to float64 isn't automatic. var a int = 1; var b float64 = float64(a). Tedious? Yes. But if you've hit "5" + 3 TypeError in production Ruby, Go's strictness starts feeling comfortable.

Ruby to Go

1

Ruby: dynamic typing, any value reassignable โ†’ Go: static typing, type locked once set

2

Ruby: nil for everything โ†’ Go: zero values per type (int=0, string="", bool=false)

3

Ruby: .to_s, .to_i for easy conversion โ†’ Go: explicit conversion via strconv package

4

:= is shorthand for declare+init. Only inside functions (package level requires var)

Pros

  • All type errors caught at compile time โ€” no more "discovered in production" pattern from Ruby
  • := shorthand lets you skip explicit types โ€” less annoying than expected

Cons

  • All type conversions explicit โ€” code gets longer compared to Ruby's .to_s one-liner
  • Generics only added in 1.18 โ€” type system still has immature areas

Use Cases

When you want to catch runtime type errors from Ruby's dynamic typing at compile time When you want to safely parse JSON API responses by declaring types as structs