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)
}
全世界的程序员们联合起来吧!
原文地址:https://www.cnblogs.com/chaojiyingxiong/p/15779691.html