Table of Contents
net/httpでサーバーを立ち上げる
DBと接続することはできたので、次はエンドポイント作成のためサーバーを立ち上げる。
サーバーを構築する組み込みのパッケージが用意されているので、そちらを使用する。
インストールの仕方がないとか思っていたけど、組み込み、つまり標準装備なので特にインストールは必要なく、importすれば使用可能だった。
で、
package main
import ( "fmt" "log" "net/http")
func homePage(w http.ResponseWriter, r *http.Request){ fmt.Fprintf(w, "Welcome to the HomePage!") fmt.Println("Endpoint Hit: homePage")}
func handleRequests() { http.HandleFunc("/", homePage) log.Fatal(http.ListenAndServe(":8081", nil))}
func main() { handleRequests()}上記をmain.goに記載したうえで、$ go run src/main.goを実行する。
http://localhost:8081/に訪れると、
Welcome to the HomePage!が表示されている。これでサーバー立ち上げの確認ができた。