golang http简单入门

package main

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

func Healthz(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	for key, value := range r.Header {
		w.Header().Set(key, value[0])
	}
	os.Setenv("VERSION", "0.0.1")
	env := os.Getenv("VERSION")
	fmt.Fprint(w, env)
	log.Printf("%s %s", r.RemoteAddr, r.Method)
}

func main() {
	http.HandleFunc("/", IndexHandler)
	http.HandleFunc("/healthz", Healthz)
	http.ListenAndServe(":8000", nil)
}
posted @ 2021-10-02 22:26  周围静地出奇  阅读(37)  评论(0编辑  收藏  举报