Skip to content

Instantly share code, notes, and snippets.

Last active December 30, 2015 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgo/7790262 to your computer and use it in GitHub Desktop.
Save jbgo/7790262 to your computer and use it in GitHub Desktop.
Comparing ruby and go

Comparison of a simple, idiomatic ruby program to a similar go program.

To run the ruby program: ruby mammals.rb

To run the go program: go run mammals.go

Read the detailed comparison on my blog.

package main
import (
type Mammal struct {
name string
group string
var mammals = []Mammal{
Mammal{"Baboon", "Primates"},
Mammal{"Dingo", "Carnivora"},
Mammal{"Bison", "Artiodactyla"},
Mammal{"Gerbil", "Rodentia"},
Mammal{"Bear", "Carnivora"},
Mammal{"Chimpanzee", "Primates"},
Mammal{"Deer", "Artiodactyla"},
Mammal{"Fox", "Carnivora"},
Mammal{"Hamster", "Rodentia"},
Mammal{"Jaguar", "Carnivora"},
func main() {
groupedMammals := make(map[string][]string)
for _, mammal := range mammals {
_, ok := groupedMammals[]
if !ok {
groupedMammals[] = make([]string, 0)
groupedMammals[] = append(groupedMammals[],
for group, animalNames := range groupedMammals {
fmt.Printf("%s: %s\n", group, strings.Join(animalNames, ", "))
class Mammal
attr_accessor :name, :group
def initialize(name, group) = name = group
mammals = ['Baboon', 'Primates'),'Dingo', 'Carnivora'),'Bison', 'Artiodactyla'),'Gerbil', 'Rodentia'),'Bear', 'Carnivora'),'Chimpanzee', 'Primates'),'Deer', 'Artiodactyla'),'Fox', 'Carnivora'),'Hamster', 'Rodentia'),'Jaguar', 'Carnivora')
grouped_mammals = {}
mammals.each do |mammal|
grouped_mammals[] ||= []
grouped_mammals[] <<
grouped_mammals.each do |group, mammal_names|
puts "#{group}: #{mammal_names.join(', ')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment