go实现http服务

package main

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

func indexHandler(w http.ResponseWriter, r *http.Request) {

    fmt.Println("打印Header参数列表:")
    if len(r.Header) > 0 {
      for k,v := range r.Header {
         fmt.Printf("%s=%s\n", k, v[0])
		 w.Header().Set(k,v[0])
      }
   }
   VERS := os.Getenv("Version")
   fmt.Printf("变量Version: %s\n",VERS)
   w.Header().Set("Version",VERS)
   fmt.Printf("客户端IP: %s\n",r.RemoteAddr)
   fmt.Println("打印response header参数列表: ")
   fmt.Println(w.Header())
   fmt.Fprintln(w, "hello world")
}

func healthz(w http.ResponseWriter, r *http.Request) {

   w.WriteHeader(http.StatusOK)
   fmt.Fprintln(w, "OK")
}


func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/healthz",healthz)
http.ListenAndServe(":8000", nil)
}

 

posted @ 2022-01-08 21:33  年轻人——001  阅读(74)  评论(0编辑  收藏  举报