はじめに
golangで__Webアプリケーションのルーティング__を実装する方法を覚えたのでアウトプットします!
実装ページ



コード
package main
import (
"fmt"
"net/http"
)
//「http://localhost:8080/hello」の処理
func helloHandle(w http.ResponseWriter, r *http.Request) {
h := `
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello
</body>
</html>
`
fmt.Fprint(w, h)
}
//「http://localhost:8080/goodbye」の処理
func goodbyeHandle(w http.ResponseWriter, r *http.Request) {
h := `
<html>
<head>
<title>goodbye</title>
</head>
<body>
goodbye
</body>
</html>
`
fmt.Fprint(w, h)
}
//「http://localhost:8080/」の処理
func landingHandle(w http.ResponseWriter, r *http.Request) {
h := `
<html>
<head>
<title>Landing</title>
</head>
<body>
Landing
</body>
</html>
`
fmt.Fprint(w, h)
}
func main() {
// URLごとに関数を登録
http.HandleFunc("/hello", helloHandle)
http.HandleFunc("/goodbye", goodbyeHandle)
http.HandleFunc("/", landingHandle)
//Webサーバを起動
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println(err)
}
}