2019年6月28日金曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 15(responding to requests - Web Apps)、Exercise(434)の解答を求めてみる。

コード

package main

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

func write(writer http.ResponseWriter, message string) {
 _, err := writer.Write([]byte(message))
 if err != nil {
  log.Fatal(err)
 }
}
func d(writer http.ResponseWriter, request *http.Request) {
 write(writer, "z")
 fmt.Println("z", request.URL)
}
func e(writer http.ResponseWriter, request *http.Request) {
 write(writer, "x")
 fmt.Println("x", request.URL)
}
func f(writer http.ResponseWriter, request *http.Request) {
 write(writer, "y")
 fmt.Println("y", request.URL)
}

func main() {
 http.HandleFunc("/a", f)
 http.HandleFunc("/b", e)
 http.HandleFunc("/c", d)
 err := http.ListenAndServe("localhost:4567", nil)
 log.Fatal(err)
}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ go run sample1.go
z /c
y /a
x /b
  C-c C-csignal: interrupt
$ 

0 コメント:

コメントを投稿

関連コンテンツ