Object-oriented programming (OOP) is a paradigm that organizes code around objects and data. While Ruby is a pure object-oriented language, Go takes a more minimalist approach to OOP, emphasizing composition over inheritance. This article compares key OOP features in Golang and Ruby.
Class Variables and Class Methods
Class variables and methods are accessible without instantiating the class.
In Ruby, class instance variables are defined with @var, and class methods are singleton methods on the class.
class A
  @class_var = "class variable"
  def self.kind
    "class method"
  end
end
puts A.kind  # Output: class method
In Golang, class variables are like package-level global variables, and class methods are functions without a receiver.
package main
import "fmt"
var classVar = "class variable"
func Kind() string {
    return "class method"
}
func main() {
    fmt.Println(Kind())  // Output: class method
}
Instance Variables and Instance Methods
Instance variables and methods are bound to object instances and called via object references.
Ruby Example:
class Person
  def initialize
    @kind = "human"
  end
  def kind
    @kind
  end
end
person = Person.new
puts person.kind  # Output: human
Golang Example:
package main
import "fmt"
type Person struct {
    kind string
}
func (p *Person) Kind() string {
    return p.kind
}
func main() {
    person := &Person{kind: "human"}
    fmt.Println(person.Kind())  // Output: human
}
Methods and Instance Variables
Methods enable logic reuse and object manipulation through inheritance and composition.
Instance variables store object attributes to differentiate instances.
In Ruby, methods can be overridden, and instance variables are dynamic.
In Golang, methods are defined on types, and fields are explicitly declared in structs.
Interfaces
Interfaces define method signatures for duck typing, focusing on functionality over type.
In Ruby, interfaces are implicit; any object with the required methods works.
def call_kind(obj)
  obj.kind
end
person = Person.new
call_kind(person)  # Works if obj responds to :kind
In Golang, interfaces are explicit.
type Kindable interface {
    Kind() string
}
func callKind(k Kindable) {
    fmt.Println(k.Kind())
}
func main() {
    person := &Person{kind: "human"}
    callKind(person)
}
Key Differences
- Inheritance: Ruby uses class-based inheritance; Go uses composition with embedding.
 - Polymorphism: Both support polymorphism, but Go uses interfaces explicitly, while Ruby uses duck typing.
 - Encapsulation: Both support, but Go has no private/public keywords; visibility is based on capitalization.
 - Dynamic vs. Static: Ruby is dynamic; Go is statically typed.
 
Conclusion
Ruby excels in dynamic, expressive OOP, while Go emphasizes simplicity and performance with a lighter OO model. Choose based on your project’s needs.