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) }
golang_nethttp的更多相关文章
随机推荐
- Codeforces 1492D、Genius's Gambit
原题网址 https://codeforces.com/contest/1492/problem/D 题目大意 给定a,b,k,求x,y使得x和y的二进制表示都恰有a个0和b个1,且不能使用开头的0. ...
- iOS中的三种定时器
iOS中的三种定时器 NSTimer 一.背景 定时器是iOS开发中经常使用的,但是使用不慎会造成内存泄露,因为NSTimer没有释放,控制器析构函数dealloc也没有调用,造成内存泄露. 二.使用 ...
- c++练习270题:三角形个数
*270题 原题传送门:http://oj.tfls.net/p/270 题解: #include<bits/stdc++.h>using namespace std;int a,b,c, ...
- IDEA Download missing driver files 下载失败解决方法
使用IDEA连接数据库时可能会遇到缺失驱动文件的问题(提示Download missing driver files) 正常情况下点击Download后IDEA会自动下载缺失的文件,但是因为网络原因, ...
- [737] Interlude OpCodez
[737] Interlude Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 RequestEnterWorld ...
- unixbench测试CPU性能工具
UnixBench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linux系统主机的性能.Unixbench的主要测试项目有:系统调用.读写.进程. ...
- com.mysql.cj.jdbc.exceptions.CommunicationsException
mysql数据库链接空闲一段时间后就会关闭连接,但是我们的java程序依然持有该连接的引用,对已经关闭的数据库连接进行操作就会引发上述异常. 解决办法: 1.增大mysql数据库的wait_timeo ...
- sxt_(015)_request处理表单数据及乱码
一.request处理表单数据及乱码 1.1 处理单个表单数据用request.getParameter("x"); 1.2 处理多个表单(checkbox.select)数据用r ...
- 【vcpkg】使用vcpkg安装库
https://blog.csdn.net/cjmqas/article/details/79282847 使用vcpkg 查看vcpkg支持的开源库列表 执行命令 .\vcpkg.exe searc ...
- win10 校验MD5值
CertUtil -hashfile C:\xxxxx\xxx.xxx MD5 // C:\xxxxx\xxx.xxx为文件路径