码迷,mamicode.com
首页 > Web开发 > 详细

go_http

时间:2019-08-09 23:36:23      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:imp   nan   writer   sts   document   pattern   ble   string   fun   

httpSvr

// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
	DefaultServeMux.HandleFunc(pattern, handler)
}

  

// ListenAndServe listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler Handler) error {
	server := &Server{Addr: addr, Handler: handler}
	return server.ListenAndServe()
}

  

package main

import (
	"fmt"
	"net/http"
)

func Hello(w http.ResponseWriter, r *http.Request) {
	fmt.Println("handle hello")
	fmt.Fprintf(w, "dashboard page")
}

func Login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("handle login")
	fmt.Fprintf(w, "login page")
}


func main() {
	http.HandleFunc("/", Hello)
	http.HandleFunc("/login/", Login)
	err := http.ListenAndServe("127.0.0.1:8000", nil)
	if err != nil {
		fmt.Println("httpSvr listen failed")
	}
}

  

httpClient

 

go_http

标签:imp   nan   writer   sts   document   pattern   ble   string   fun   

原文地址:https://www.cnblogs.com/jabbok/p/11330010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!