go Server示例

示例1:

package main

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

func main() {

	fmt.Println("Please visit http://127.0.0.1:8880/")
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		s := fmt.Sprintf("你好 世界 -- Time:%s", time.Now().String())
		fmt.Fprintf(w, "%v\n", s)
		log.Printf("%v\n", s)
	})

	if err := http.ListenAndServe(":8880", nil); err != nil {
		log.Fatal("ListenAndServer: ", err)
	}

}

示例2:

package main

import (
	"fmt"
	"net/http"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello")
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Printf("http server failed, err:%v\n", err)
		return
	}
}


posted on 2019-09-04 23:22  action555  阅读(310)  评论(0编辑  收藏  举报