A Simple Program to Help You Go From Ruby To Go

Dabbling With the Go Programming Language

With the rise of Go, I’ve been dabbling with it more and more. If you’re a Rubyist and you’re wondering how to setup Golang on your system, check out my article Setting Up Golang Like a Rubyist.

The High Level Basics

Here’s a few things that are helpful to know as you’re starting Go:

  • Go has a built in testing package
  • There’s a precise directory structure you must obey (covered in Setting Up Golang Like a Rubyist.)
  • There are no classes in Go, but you can have Structs
  • The else in an if/else statement goes right after the ending curly brace as follows:

1
2
if flag==true {
3
  //some code
4
} else {
5
  //some code
6
}
7

A Sample Problem to Walk Through

Here I’m going to implement a function to determine if a string’s characters are all unique. The function will return true if the string contains only unique characters and false otherwise.

The Golang Code

Below is the source code for a naive implementation of a function that solves the problem.

arrays_and_strings.go

01
02
package arrays_and_strings
03
 
04
import "strings"
05
 
06
func uniqueString(s string) bool {
07
	var (
08
		flag bool = true
09
	)
10
	for _, r := range s {
11
		if strings.Count(s, string(r)) > 1 {
12
		  flag = false
13
		}
14
	}
15
	return flag
16
}
17

The Golang Test Code

Below is the code that tests the source code in arrays_and_strings.go.

arrays_and_strings_test.go

01
02
package arrays_and_strings
03
 
04
import (
05
	"testing"
06
)
07
 
08
func TestUniqueStringCharacters(t *testing.T) {
09
  got := uniqueString("abcdxyz")
10
	if got == false {
11
	  t.Fatalf("uniqueString failed")
12
	}
13
}
14
 
15
func TestDuplicateStringCharacters(t *testing.T) {
16
  got := uniqueString("aabcdxyz")
17
	if got == true {
18
	  t.Fatalf("uniqueString failed")
19
	}
20
}
21

A Few Words About The Test Code

You’ll notice the t *testing.T parameter. This appears to be the default structure for tests in Go. It enables you to output a failure message if the test fails. This is useful to know if you’re practicing test driven development (i.e., write a failing test first, then make it pass).

Differences from testing in Ruby

The other observation to point out is that the Go testing framework appears to not let you print messages using its fmt.Println statement (which is basically like puts in Ruby) when running tests.

For example, I tried:

01
02
func TestDuplicateStringCharacters(t *testing.T) {
03
  got := uniqueString("aabcdxyz")
04
  if got == true {
05
    t.Fatalf("uniqueString failed")
06
  } else {
07
    fmt.Println("test passed")
08
  }
09
}
10

This is an example of Go enforcing strict configuration. I’m not sure how I feel about it, but it’s a definite change from the flexibility of Ruby.

I do like the fact that Go compiles down to any system, and because it’s a compiled language, right out of the box you’re going to much better performance than an interpreted language such as Ruby. But I still like Ruby anyway. After all, I could have solved the problem in Ruby with the following one line snippet in a method:

1
2
def char_uniq?(sample_string = "aBcdE")
3
  ("a".."z").none? {|c| sample_string.downcase.count(c) > 1 }
4
end
5

Collection of My Favorite Ruby and Rails Performance and Memory Articles

This is a collection of my favorite articles on Ruby and Rails performance/memory issues. I’ll be updating it from time to time.

Benchmarking / Performance

http://www.schneems.com/2014/10/31/benchmarking-rack-middleware.html

http://www.sitepoint.com/rubygems-slow/

ActiveRecord

http://aokolish.me/blog/2015/05/26/how-to-simplify-active-record-scopes-that-reference-other-tables/

Memory

http://www.sitepoint.com/ruby-uses-memory/ – how Ruby uses memory

https://blog.engineyard.com/2009/thats-not-a-memory-leak-its-bloat/ – bloat

http://www.schneems.com/2014/11/07/i-ram-what-i-ram.html – 36% smaller Rails memory footprint through Benchmarking

Heap dumps for debugging memory leaks

http://gavinmiller.io/2015/debugging-memory-leaks-on-heroku/

http://blog.skylight.io/hunting-for-leaks-in-ruby/

http://samsaffron.com/archive/2015/03/31/debugging-memory-leaks-in-ruby

How to get process id of Rails running locally

1
2
ps -aef | grep rails
3
 
4
bruce     4679  4433  1 12:10 pts/17   00:01:17 /home/bruce/.rvm/rubies/ruby-2.2.1/bin/ruby bin/rails s -p 3001
5
bruce     5030  3378  0 12:21 pts/0    00:00:11 /home/bruce/.rvm/rubies/ruby-2.2.1/bin/ruby bin/rails s
6

http://www.scimedsolutions.com/articles/36-rbtrace-is-awesome

bundle exec rbtrace -p 11697 -u 300 -e ‘Thread.new{require “objspace”; ObjectSpace.trace_object_allocations_start; GC.start(); ObjectSpace.dump_all(output: File.open(“heap.json”, “w”))}.join’

Setting Up Golang Like a Rubyist

Getting into the Go Programming LanguageI use Ruby in my day job, but recently I’ve been dabbling with Go. The install docs give you an easy way to setup if you’re on Linux:export PATH=$PATH:/usr/local/go/binBut I’m used to RVM and

Continue Reading …

My Year of Software – A Review of 2014

Ten software lessons in 2014Lesson 1 - Building a Rails APIIn 2014, I built a backedn Rails 4.2 API using the rocket_pants gem and used another Rails application to view the API data. I learned to watch how many objects I was creating and

Continue Reading …

Camel Casing Your API Response in rocket_pants

What is rocket_pants?Rocket pants is a gem that gives you a set of tools for building an API in Ruby with Rails.Motivation for camel casingThe developer team decided we wanted to return camel-cased json responses, e.g.,{

Continue Reading …

Testing Elasticsearch In Your Rails 4 Application With RSpec

What is Elasticsearch? Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library.If you haven’t setup elasticsearch with your Rails application, you can read about

Continue Reading …

How to Implement jQuery Colorbox

How to Implement jQuery ColorboxWhat is jQuery Colorbox?jQuery Colorbox is a lightbox plugin that supports “photos, grouping, slideshow, ajax, inline, and iframed content.” In a nutshell, you can create nice looking popup boxes

Continue Reading …

How to Add jQuery UI Datepicker To Your Rails 4 Application With Formtastic and Cocoon

What is Formtastic, Cocoon, and jQuery UI Datepicker?Formtastic is a gem that lets you add forms more easily to your Rails application. Cocoon allows you to add dynamic nested forms to your Rails application using jQuery. jQuery-ui Datepicker

Continue Reading …

How To Move an Existing Github Repository to Bitbucket

Moving an Existing Github Repository to BitbucketHere is the link to the procedure on coderwall…

Continue Reading …

How To Save Nested Attribute Checkboxes With Formtastic and Cocoon and Avoid the TypeError

Avoiding the TypeError: can’t cast Array to stringRecently, I was inserting nested attribute checkboxes using Formtastic and Cocoon and was cruising along with all my other nested forms partials until I got a dreaded TypeError: can’t cast Array

Continue Reading …