DEV Community

Cover image for Learning Go as a Ruby Developer # 1: How Go Programs Actually Run
Shrouk Abozeid
Shrouk Abozeid

Posted on

Learning Go as a Ruby Developer # 1: How Go Programs Actually Run

Recently I started learning Go, and one of the first things I noticed was how much it reminded me of C++.
I haven't touched C++ since college, so Go gives me a weird sense of nostalgia :D

As a Ruby developer, I'm used to writing code and simply executing it.
Go, however, has a much stronger notion of what a program actually is and how it starts running.

In this article, I want to explore how a Go program is structured and compare it with how we typically think about programs in Ruby

Why This Felt Strange Coming from Ruby

In Ruby, writing a program is straightforward:

puts "Hello World"

Save it as hello.rb and run:

ruby hello.rb

There's no special package declaration, no imports required for basic programs, and no designated entry point function.

Go takes a very different approach.

Every executable Go program follows a specific structure, and understanding this structure is one of the first steps to becoming comfortable with the language.

How Go Programs Are Structured

A typical Go file looks like this:

package main
import (
 "fmt" 
) 

func main() {
 fmt.Println("Hello World")
}
Enter fullscreen mode Exit fullscreen mode

There are three important pieces here:

1. Package Declaration

Every Go file begins with a package declaration:

package main

Packages are Go's mechanism for organizing code.

If you want to build an executable application, the package must be named main.

2. Imports

Go requires explicit imports:

import (
    "fmt"
)
Enter fullscreen mode Exit fullscreen mode

The compiler will actually complain if you import something you don't use.

As a Ruby developer, this felt unusual because Ruby happily allows unused
requires:

require 'json'
require 'csv'

puts "Hello"
Enter fullscreen mode Exit fullscreen mode

Go prefers keeping dependencies explicit and clean.

3. The main Function

Every executable Go program starts execution in:

func main() {
}
Enter fullscreen mode Exit fullscreen mode

The mainfunction:

  • accepts no arguments
  • returns no values
  • acts as the application's entry point

When main() finishes, the program exits.

How Program Execution Works

According to the Go specification, a complete program consists of:

  • one package named main
  • all packages imported by main
  • all transitive dependencies

Program execution happens in two stages:

  1. Initialize packages
  2. Execute main()

Once main()returns, the program terminates.

Running vs Building Programs

Go provides two common ways to execute code.

Run directly

go run hello.go

This compiles and runs the program immediately.

Build an executable

go build hello.go
./hello
Enter fullscreen mode Exit fullscreen mode

This creates a standalone binary.

In Ruby: applications are distributed as source code and interpreted at runtime.

Coming from Rails, where so much setup happens automatically, Go feels so explicit.

At the same time, that explicitness means you have to think about things that Ruby usually hides from you.

while Ruby optimizes for developer happiness and speed of iteration, Go forces me to understand what's actually happening.

What about you? do you use Ruby or Go, or both ? and how was the transition for you ?

Top comments (0)