go-web服务器搭建

转载学习 

https://www.cnblogs.com/franklee97/p/7131551.html 

 https://my.oschina.net/zlLeaf/blog/173035

package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello Go, %s!", r.URL.Path[1:])
}
func main()  {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
package main

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

func sayHelloGO(w http.ResponseWriter, r *http.Request)  {
	r.ParseForm()
	fmt.Println(r.Form)
	fmt.Println("path", r.URL.Path)
	fmt.Println("scheme", r.URL.Scheme)
	for k, v := range r.Form {
		fmt.Println("key", k)
		fmt.Println("val", strings.Join(v, ""))
	}
	fmt.Fprintf(w, "hello Go!")
}
func main()  {
	http.HandleFunc("/",sayHelloGO)
	err := http.ListenAndServe(":8080",nil)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}

猜你喜欢

转载自blog.csdn.net/czy279470138/article/details/90081124