dev/mom0tomo

技術メモ

Sinatra(Ruby)とGoの標準パッケージで立てたサーバを見比べる

たまたま同じ時期に0からサーバーを立てる機会に恵まれて、似てるなと思ったので比較してみる。

Sinatra

app.rb

require 'sinatra'

get '/' do
  erb :index
end

index.erb

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>タイトル</title>
  </head>
  <body>
    <p>Sinatraだよ</p>
  </body>
</html>

Go

init.go

package main

import "net/http"

func init() {
    http.HandleFunc("/", index)
}

index.go

package main

import (
    "html/template"
    "net/http"
)

var indexTmpl = template.Must(template.New("index").Parse(`<!DOCTYPE html>
<html>
  <head>
      <meta charset="UTF-8">
      <title>タイトル</title>
  </head>
  <body>
      {{.}}
  </body>
</html>`))

func index(w http.ResponseWriter, r *http.Request) {
    fmt.Println(w, "Goだよ")
}

改めてGoの標準パッケージすごい。そして html/templateつらい。