• golang_nethttp


    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"log"
    	"net/http"
    )
    //<link rel="stylesheet" href="media/index.css">
    func indexHandler(w http.ResponseWriter, r *http.Request) {
    	http.ServeFile(w,r,"index.html")
    
    }
    //http://127.0.0.1:8000/submit
    func submitHandler(w http.ResponseWriter, r *http.Request) {
    	//获取post json请求转换为map
    	err := r.ParseForm()
    	if err != nil {
    		log.Fatal("parse form error ", err)
    	}
    	// 初始化请求变量结构
    	formData := make(map[string]interface{})
    	// 调用json包的解析,解析请求body
    	json.NewDecoder(r.Body).Decode(&formData)
    
    	//log.Println("req form: ", formData)
    	for key, value := range formData {
    		log.Println("key:", key, " => value :", value)
    	}
    	fmt.Fprintf(w, "submitHandler")
    
    }
    func main() {
    	http.HandleFunc("/", indexHandler)
    	http.HandleFunc("/submit", submitHandler)
    	http.Handle("/media/",http.StripPrefix("/media/",http.FileServer(http.Dir("./media"))))
    	println("web server started sucess")
    	http.ListenAndServe(":8000", nil)
    
    }
    

      


     //media/index.css
    body {
        background-color:#d0e4fe;
    }
    h1 {
        color:orange;
        text-align:center;
    }
    p {
        font-family:"Times New Roman";
        font-size:20px;
    }
    

      

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>菜鸟教程(runoob.com)</title>
        <link rel="stylesheet" href="media/index.css">
    </head>
    
    <body>
    
    <h1>CSS 实例!</h1>
    <p>这是一个段落。</p>
    
    </body>
    </html>
    

      

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"log"
    	"net/http"
    	"text/template"
    )
    //<link rel="stylesheet" href="media/index.css">
    func indexHandler(w http.ResponseWriter, r *http.Request) {
    	// 加载html模板
    	t, _ := template.ParseFiles("index.html")
    	// 准备模板数据
    //	data := map[string]interface{}{"userName": "irene", "count": 123}
    	// 渲染模板数据并相应页面
    	t.Execute(w, nil)
    	//http.ServeFile(w,r,"index.html")
    
    
    }
    //http://127.0.0.1:8000/submit
    func submitHandler(w http.ResponseWriter, r *http.Request) {
    	//获取post json请求转换为map
    	err := r.ParseForm()
    	if err != nil {
    		log.Fatal("parse form error ", err)
    	}
    	// 初始化请求变量结构
    	formData := make(map[string]interface{})
    	// 调用json包的解析,解析请求body
    	json.NewDecoder(r.Body).Decode(&formData)
    
    	//log.Println("req form: ", formData)
    	for key, value := range formData {
    		log.Println("key:", key, " => value :", value)
    	}
    	fmt.Fprintf(w, "submitHandler")
    
    }
    func main() {
    	http.HandleFunc("/", indexHandler)
    	http.HandleFunc("/submit", submitHandler)
    	http.Handle("/media/",http.StripPrefix("/media/",http.FileServer(http.Dir("./media"))))
    	println("web server started sucess")
    	http.ListenAndServe(":8000", nil)
    
    }
    

      

  • 相关阅读:
    AOP从静态代理到动态代理 Emit实现
    Emit基础入门 系列文章
    ContextAttribute与ContextBoundObject应用的探究
    吞云吐烦忧
    动态编译代码框架发布CZGL.Roslyn
    AutoMapper 入门
    Attribute在.net编程中的应用
    C#反射与特性 系列文章
    静态代理和动态代理方式分别实现AOP拦截功能
    内网安全攻防:渗透测试指南——第8章 权限维持分析及防御
  • 原文地址:https://www.cnblogs.com/leochan007/p/16782476.html
Copyright © 2020-2023  润新知