zoukankan      html  css  js  c++  java
  • (六)ServeMux示例

    package main
    
    import (
    	"fmt"
    	"net/http"
    )
    
    type DB struct{
    	name string
    	age int
    }
    
    var gdb DB = DB{"catty",26}
    
    func get(w http.ResponseWriter, req *http.Request) {
    	// The "/" pattern matches everything, so we need to check
    	// that we're at the root here.
    	if req.URL.Path != "/get" {
    		http.NotFound(w, req)
    		return
    	}
    	fmt.Fprintf(w, "Welcome to the home page!
    ")
    	fmt.Fprintf(w, "name is %s, age is %d
    ",gdb.name,gdb.age)
    }
    
    func (db *DB)list(w http.ResponseWriter, req *http.Request) {
    	if req.URL.Path != "/list" {
    		http.NotFound(w, req)
    		return
    	}
    	fmt.Fprintf(w, "Welcome to the home page!
    ")
    	fmt.Fprintf(w, "name is %s, age is %d
    ",db.name,db.age)
    	w.Write([]byte("json message"))
    }
    
    
    func main(){
    	db:=DB{"doggy",27}
    	mux := http.NewServeMux()
    	mux.HandleFunc("/get",get)
    	mux.HandleFunc("/list",db.list)
    	http.ListenAndServe("localhost:1234",mux)
    }
    
    
  • 相关阅读:
    PHP curl_exec函数
    PHP curl_escape函数
    PHP curl_error函数
    PHP curl_errno函数
    PHP curl_copy_handle函数
    PHP curl_close函数
    PHP 利用 curl 发送 post get del put patch 请求
    PHP cURL 函数
    PHP 实例
    PHP 实例
  • 原文地址:https://www.cnblogs.com/walkinginthesun/p/10211147.html
Copyright © 2011-2022 走看看