Skip to content

Instantly share code, notes, and snippets.

@sathiyaseelan
Last active December 3, 2017 22:25
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 sathiyaseelan/76a7495180b3e7949238b16ced37175b to your computer and use it in GitHub Desktop.
Save sathiyaseelan/76a7495180b3e7949238b16ced37175b to your computer and use it in GitHub Desktop.
Golang and Elixir comparision with respect to building JSON APIs

Spent some time to understand go and (elixir + (phoenix)) with respect to write APIs. I’ve considered only syntax, developer productivity and features like ORM.

Golang

Pros

  1. Faster than Elixir.
  2. GORM provides similar functionality as ActiveRecord

Cons

  1. The syntax is similar to C or java(But eventually, we will get used to that.)
  2. But I really doubt that we will match the productivity of Rails.

Elixir + Phoenix

Pros

  1. Faster than Rails
  2. Better Productivity compared to Golang.

Cons

  1. Not as fast as Golang.( But which may not be a problem for many our use cases)
  2. Ecto is not ActiveRecord (But still managable)
// Sample JSON API
func GetPerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for _, item := range people {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Person{})
}
func DeletePerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for index, item := range people {
if item.ID == params["id"] {
people = append(people[:index], people[index+1:]...)
break
}
json.NewEncoder(w).Encode(people)
}
}
// main function to boot up everything
func main() {
router := mux.NewRouter()
people = append(people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}})
router.HandleFunc("/people/{id}", GetPerson).Methods("GET")
router.HandleFunc("/people/{id}", DeletePerson).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8000", router))
}
// Sample GORM code
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
// Migrate the schema
db.AutoMigrate(&Product{})
// Create
db.Create(&Product{Code: "L1212", Price: 1000})
// Read
var product Product
db.First(&product, 1) // find product with id 1
db.First(&product, "code = ?", "L1212") // find product with code l1212
}
# Sample Ecto code
defmodule Sample.Weather do
use Ecto.Schema
schema "weather" do
field :city # Defaults to type :string
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
end
end
defmodule Sample.App do
import Ecto.Query
alias Sample.Weather
alias Sample.Repo
def keyword_query do
query = from w in Weather,
where: w.prcp > 0 or is_nil(w.prcp),
select: w
Repo.all(query)
end
def pipe_query do
Weather
|> where(city: "Kraków")
|> order_by(:temp_lo)
|> limit(10)
|> Repo.all
end
end
# Sample Endpoint code
defmodule ApiExample.UserController do
use ApiExample.Web, :controller
def index(conn, _params) do
users = Repo.all(ApiExample.User)
json conn, users
end
end
@boddhisattva
Copy link

boddhisattva commented Dec 3, 2017

Not as fast as Golang.( But which may not be a problem for many our use cases)

Agreed that Go Lang has better Raw performance. Even I have a feeling that we may not need that much performance if we use Elixir. Also, if we ever have to get that kind of Raw Performance in don't know how many hears from now, I believe Elixir and Phoenix's performance will only get better and also I think that we would be able to C extensions to Elixir if there's ever a need for that kind of perfromance.

@sathiyaseelan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment