Packages & Modules โ When Gemfile Becomes go.mod
Ruby's require/gem System vs Go's package/module System
To use external libraries in Ruby: add gem 'rails' to Gemfile and run bundle install.
In Go: run go get github.com/gin-gonic/gin and it's auto-added to go.mod.
Gemfile vs go.mod
Ruby's Gemfile lists gem names with version constraints: gem 'rails', '~> 7.0'
Go's go.mod lists module paths (usually GitHub URLs) with versions: require github.com/gin-gonic/gin v1.9.1
Biggest difference: RubyGems is a central registry where gem install rails works by name alone. Go has no central registry โ GitHub URLs are the package identifiers.
require vs import
Ruby: require 'json' โ loads by filename or gem name.
Go: import "encoding/json" โ imports by package path. Unused imports cause compile errors. Ruby doesn't care if you require something you don't use.
Directory = Package
In Ruby, file structure and module/class structure are loosely connected (Rails autoload handles it). In Go, one directory = one package. All .go files in the same directory must have the same package declaration.
GOPATH โ Go Modules
Old Go required all code in a fixed GOPATH directory. Like being forced to develop all projects in ~/ruby_projects/. Now Go Modules (go.mod) lets you work anywhere. The problem Bundler solved โ Go solved it in 2019.
Ruby to Go
Ruby: Gemfile + bundle install โ Go: go.mod + go get
Ruby: require "json" โ Go: import "encoding/json" (compile error if unused)
Ruby: RubyGems central registry โ Go: GitHub URL is the package name
Ruby: loose file/module coupling (autoload) โ Go: directory = package (1:1 mapping)
Pros
- ✓ Package downloads are fast โ fetching source directly makes builds simple
- ✓ Unused imports cause compile errors โ code stays clean automatically
Cons
- ✗ GitHub URL-based so dependencies break if repositories are deleted/moved
- ✗ Private repo dependency setup is more cumbersome than RubyGems