LoginSignup
0
0

やったこと

HTMLテンプレートファイルを読み込ませてブラウザ上で表示

ソース

package main

import (
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"sync"
)

type templateHandler struct {
	once     sync.Once
	filename string
	templ   *template.Template
}

// HTTPリクエストの処理
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t.once.Do(func() {
		t.templ =
			template.Must(template.ParseFiles(filepath.Join("templates",
				t.filename)))
	})
	t.templ.Execute(w, nil)
}

func main() {
	http.Handle("/", &templateHandler{
		filename: "chat.html"})
	//Webサーバーの開始
	if err := http.ListenAndServe(":8081", nil); err != nil {
		log.Fatal("ListenerAndServe:", err)
	}
}

実行結果

image.png

参考

オライリー 「Go言語によるアプリケーション開発」

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0