go_http

Posted jabbok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go_http相关的知识,希望对你有一定的参考价值。

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 := &ServerAddr: 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的主要内容,如果未能解决你的问题,请参考以下文章