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) }

  

golang_nethttp的更多相关文章

随机推荐

  1. MySql创建高性能的索引

    创建高性能的索引1.树 减少数据的查询次数二叉树 平衡树 b树 节点存储key和datab+树 节点只存储key 叶子节点存储data innodb使用b+树 当页最大16kb可以存储1000个key ...

  2. 【剑指Offer】【数组】数组中出现次数超过一半的数字

    题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如果 ...

  3. Telnet 连接smtp协议手动发邮件

    下面实验以QQ邮箱为例(比较多人用,其它邮箱也一样) Step1.打开CMD 输入:telnet SMTP服务器 SMTP端口地址   连接smtp服务器 Step2. 输入:helo 内容 (向它( ...

  4. Spring的AOP源码解析(二)

    Spring AOP 源码解析 目录 Spring AOP 源码解析 前言 本文使用的调试代码 IOC 容器管理 AOP 实例 ProxyFactory 详解 基于注解的 Spring AOP 源码分 ...

  5. Lnmp切换PHP版本

    进入lnmp的安装目录 cd /root/lnmp1.8 执行 ./install.sh mphp 然后选择你要变更的版本

  6. 跨域出现:XMLHttpRequest cannot load错误

    1.右击浏览器图标-->属性-->快捷方式-->目标栏添加--> --args --disable-web-security --user-data-dir 2.修改之后重新打 ...

  7. Typora的一些基础用法

    Typora的简单实用技巧 标题 标题分为h1~h6六个等级,数字越小标题越大. 定义标题有一下几种方式. 方式一:这个标题手敲就在文本前边敲#号,#和文本中间需又空格隔开. 方式二:快捷键,Ctrl ...

  8. ubuntu下安装使用jupyter

    sudo snap install jupyter安装,jupyter notebook使用,到浏览器访问服务URL这种使用方式不再赘述. 如何在服务端开启jupyter服务供客户端使用有待研究. 本 ...

  9. word生成产生错误的原因

    技术背景 很多时候我们网站或者系统需要提供一些word文件,例如证明.docx或者订单.docx等文件供用户下载打印等. 用Java操作word文档,毫无疑问,当下最流行apache poi,对于po ...

  10. [ZZH]第一篇博客

    time: 2022/3/29  20:24 my first blog