LoginSignup
4
5

More than 3 years have passed since last update.

【Go】http.HandleFunc()とhttp.Handle()の使い分けは構造体のフィールドを使うかどうか

Last updated at Posted at 2020-09-13

はじめに

http.HandleFunc()とhttp.Handle()でServeHTTPを呼んでいるのに
なにが違っているのか分からなかったので調べてみました。

結論

構造体のフィールドを使いたいかどうか

http.HandleFunc()とhttp.Handle()

GoのドキュメントのPackage httpのよると

ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux:

とどちらもDefaultServeMuxのパターンとしてハンドラを登録するもの。

DefaultServeMuxについてやハンドラとの関係性については以下の記事がわかりやすいです。
【Go】net/httpパッケージを読んでhttp.HandleFuncが実行される仕組み

http.HandleFunc()

ドキュメントの例では

package main

import (
    "io"
    "log"
    "net/http"
)

func main() {
    h1 := func(w http.ResponseWriter, _ *http.Request) {
        io.WriteString(w, "Hello from a HandleFunc #1!\n")
    }
    h2 := func(w http.ResponseWriter, _ *http.Request) {
        io.WriteString(w, "Hello from a HandleFunc #2!\n")
    }

    http.HandleFunc("/", h1)
    http.HandleFunc("/endpoint", h2)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

という形で
/のパターンであればHello from a HandleFunc #1!を出力し
/endpointのパターンであればHello from a HandleFunc #2!を出力する

http.Handle()

ドキュメントの例では

package main

import (
    "fmt"
    "log"
    "net/http"
    "sync"
)

type countHandler struct {
    mu sync.Mutex // guards n
    n  int
}

func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    h.mu.Lock()
    defer h.mu.Unlock()
    h.n++
    fmt.Fprintf(w, "count is %d\n", h.n)
}

func main() {
    http.Handle("/count", new(countHandler))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

という形で
/countのパターンであればカウントの回数を出力する

この時にstructのフィールドn intに呼ばれた回数ごとに+1しているので
呼ばれるたびに回数を増加することができる。

まとめ

ServeHTTPを実装しhttp.Handle()を使うことでstruct内のフィールドを利用したハンドラ作成が可能となる。

参考文献

ありがとうございました。
GoのドキュメントのPackage http
【Go】net/httpパッケージを読んでhttp.HandleFuncが実行される仕組み
Go 言語の http パッケージにある Handle とか Handler とか HandleFunc とか HandlerFunc とかよくわからないままとりあえずイディオムとして使ってたのでちゃんと理解したメモ

4
5
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
4
5