루비 개발자로 배우는 Go # 2: 변수, 상수 및 Go가 당신을 신뢰하지 않는 이유

작성자

카테고리:

← 피드로
DEV Community · Shrouk Abozeid · 2026-07-25 개발(SW)
Cover image for Learning Go as a Ruby Developer # 2: Variables, Constants, and Why Go Doesn't Trust You

Shrouk Abozeid

One of the first differences I noticed when learning Go was how seriously it takes types.

Coming from Ruby. I rarely think about their types because Ruby figures everything out at runtime.

Go has a very different philosophy.

It wants to know exactly what every variable is, and if it doesn’t know, it’ll ask you to be explicit.

Why This Felt Strange Coming from Ruby

In Ruby, creating variables couldn’t be easier.

name = "Alice"
age = 30
admin = true

Enter fullscreen mode Exit fullscreen mode

Ruby decides the type at runtime.

Later I can even do something like this:

value = 10
value = "hello"
value = [1, 2, 3]

Enter fullscreen mode Exit fullscreen mode

The same variable can hold completely different kinds of objects throughout the program.

Go doesn’t allow that.

Once a variable has a type, that type is fixed.

Declaring Variables in Go

The most explicit way to declare a variable is by specifying its type.

var age int = 30
var name string = "Alice"
var admin bool = true

Enter fullscreen mode Exit fullscreen mode

This is checked during compilation, long before your program ever runs.

If you later try to assign a different type, the compiler will stop you immediately.

This felt a bit strict—but it also means many bugs are caught before the program starts.

Type Inference

Fortunately, Go doesn’t always require you to write the type yourself.

It can often infer it.

var admin = false

Enter fullscreen mode Exit fullscreen mode

Go knows this is a bool.

Likewise:

var message = "Hello World"

Enter fullscreen mode Exit fullscreen mode

becomes a string.

Short Variable Declaration

Inside functions, Go provides an even shorter syntax.

message := "Hello World"
count := 10

Enter fullscreen mode Exit fullscreen mode

The := operator both declares the variable and lets Go infer its type.

If you’re writing local variables, you’ll probably use this style most of the time.

One thing I learned from my notes is that this syntax can only be used inside functions.

Declaring Multiple Variables

Go also makes it easy to declare multiple variables.

var x, y, z int = 12, 15, 10

Enter fullscreen mode Exit fullscreen mode

or with inferred types:

var a, b, c = "hi", 30, true

Enter fullscreen mode Exit fullscreen mode

For larger groups, Go encourages this style:

var (
    host string = "localhost"
    port int = 8080
    debug bool = true
)

Enter fullscreen mode Exit fullscreen mode

Constants

Constants are declared using const.

const MaxUsers = 100

Enter fullscreen mode Exit fullscreen mode

You can also group them.

const (
    Enabled = true
    Pi = 3.14
)

Enter fullscreen mode Exit fullscreen mode

Unlike variables, constants cannot be changed after they’re declared.

Ruby has constants too:

MAX_USERS = 100

Enter fullscreen mode Exit fullscreen mode

But every Ruby developer knows the truth…

Ruby will happily let you change it.

MAX_USERS = 200

Enter fullscreen mode Exit fullscreen mode

You’ll get a warning, not an error.

Go, on the other hand, simply refuses.

Zero Values

If you declare a variable without assigning anything, Go automatically initializes it.

var age int
var active bool
var name string

Enter fullscreen mode Exit fullscreen mode

The values become:

Type Zero Value int 0 float64 0.0 bool false string “”

Coming from Ruby, I expected something similar to nil.

Instead, Go gives every type a sensible default.

That design avoids a lot of unnecessary initialization code.

Type Conversion

Another thing that immediately stood out was that Go doesn’t automatically convert between types.

Suppose we have:

var i int = 45

Enter fullscreen mode Exit fullscreen mode

Converting it to a floating-point number requires an explicit conversion.

var f float64 = float64(i)

Enter fullscreen mode Exit fullscreen mode

Likewise:

var u uint = uint(f)

Enter fullscreen mode Exit fullscreen mode

Every conversion must be intentional.

Ruby is much more forgiving.

number = "45"

number.to_i
number.to_f

Enter fullscreen mode Exit fullscreen mode

And in many cases Ruby performs conversions automatically for you.

Go deliberately avoids this.

Creating Values with make

One line in my notes that initially confused me was this:

numbers := make([]int, 3)

Enter fullscreen mode Exit fullscreen mode

Unlike arrays in many languages, make creates and initializes certain built-in data structures.

In this example, it creates a slice with a length of three.

I’ll dive much deeper into slices later in the series, but for now it’s enough to know that make is commonly used when creating slices, maps, and channels.

So to conclude:

Changing a type?

Be explicit.

Creating a variable?

Be explicit—or let the compiler infer it.

Converting numbers?

Be explicit.

At first it felt like the language didn’t trust me 😀

Now I think it’s more accurate to say Go doesn’t trust assumptions.

Instead of guessing what I meant, it asks me to say exactly what I want.

Ruby optimizes for flexibility.
Go optimizes for correctness.

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다