I rewrote my blog in Go

  • 2,157 views
Uploaded on

A summary of my experiences in rewriting the IronZebra blog from Django to Go. For the original HN front-page blog post, see http://www.ironzebra.com/code/103/i-rewrote-my-blog-in-go

A summary of my experiences in rewriting the IronZebra blog from Django to Go. For the original HN front-page blog post, see http://www.ironzebra.com/code/103/i-rewrote-my-blog-in-go

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,157
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
16
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Rewriting a blog in Go Streamlining from Django to Go
  • 2. My background PHP JavaScript CSS Java C++ Python Node.js Go →
  • 3. A bit about Go ➔ Syntax somewhere between Python and Java package main import "fmt" func main() { fmt.Println("Hello, 世界") }
  • 4. A bit about Go ➔ Compiled language ➔ Statically typed, but not stupid: var hello string = “hi there” // valid greeting := “how are you” // also valid ➔ Good compromise between ease of development and efficiency
  • 5. A bit about Go ➔ Very opinionated... $ go fmt yourcode.go $ go get github.com/hermanschaaf/ironzebra ...and that’s a good thing
  • 6. ➔ Learning & Fun ➔ The “Hello World” of web applications ➔ I did the same thing in Django ➔ The Django blog was reaaally sloooooow ➔ Fear of landing on HN front page (really!) Reasons for rewriting my blog
  • 7. What I want ➔ Markdown ➔ MongoDB ➔ Admin Login ➔ Faster load times
  • 8. What I go get ➔ Markdown (github.com/russross/blackfriday) ➔ MongoDB (labix.org/v2/mgo) ➔ Admin Login (code.google.com/p/go.crypto/bcrypt) ➔ Faster load times (github.com/robfig/revel)
  • 9. Markdown: blackfriday $ go get github.com/russross/blackfriday import "github.com/russross/blackfriday" // markdown string body := “Hello, _markdown_, my old *friend*.” // parse markdown into HTML string html := string(blackfriday.MarkdownCommon([]byte (body)))
  • 10. MongoDB: mgo ➔ mgo is a great wrapper for MongoDB func getPosts(c Blog) ([]models.Post) (postList [] models.Post{}) { collection := c.Database.C("posts") query := bson.M{"published": true} q := collection.Find(query).Sort("-timestamp") q.Limit(10).Iter().All(&postList) }
  • 11. MongoDB: mgo ➔ ...but had some surprises: collection.Find( bson.M{"title": title}).One(&result)
  • 12. Faster load times: Revel http://www.techempower.com/benchmarks/#section=data-r5&hw=i7&test=db&o=5&f=1elc8-9fm70g-0
  • 13. Revel ➔ MVC framework ➔ Still in pre-1.0 release ➔ Offers routing, parameter parsing, validation, session/flash, templating, caching, job running, testing, and even i18n
  • 14. Revel ➔ Really nice routing: GET / App.Index GET /posts/ Blog.List GET /:category/:id/:slugString Blog.Show ➔ Useful controller hooks ➔ Each http request gets separate goroutine
  • 15. Revel ➔ Go’s built-in templates aren’t so great [[--- show.html ---]] {{set . "title" .result.Title}} {{template "header.html" .}} </head> {{template "post.html" .}} {{ .html_output }}
  • 16. The Results Time Googlebot spent downloading the HTML text only
  • 17. BUT!! ➔ This was not a scientific experiment: ◆ Moved server to Heroku (git deploy!) ◆ Trimmed down the HTML, removed some queries ◆ Database used to be on same machine, now hosted on Mongolab
  • 18. Conclusions ➔ Site loading at least 10x faster ➔ Same amount of effort as Django, or less ➔ “Hello World” of web applications: Proof of concept of what can be done when not even trying very hard
  • 19. I no longer fear the HN frontpage