- A+
所属分类:编程茶楼
最简单的web服务
package main
import (
"fmt"
"net/http"
)
func myWeb(w http.ResponseWriter,r *http.Request){
fmt.Fprintf(w,"这是一个开始")
}
func main(){
http.HandleFunc("/",myWeb)
fmt.Println("服务器即将开启,访问地址 http://localhost:830")
err := http.ListenAndServe(":830",nil)
if err !=nil {
fmt.Println("服务器开启错误:",err)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Web互动第一步,Go http 获得请求参数
还是先从代码开始
package main
import (
"fmt"
"net/http"
)
func myWeb(w http.ResponseWriter,r *http.Request){
r.ParseForm() //它还将请求主体解析为表单,填充数据到 r.Form 和 r.PostForm
for k,v :=range r.URL.Query(){
fmt.Println("key:",k,"value:",v[0],v[1]) //同名参数会以数组存储值
}
for k,v:=range r.PostForm{
fmt.Println("key:",k,"value:",v[0],v[1]) //同名参数会以数组存储值
}
//r.URL.Query() 和 r.PostForm 分别是URL参数对象和表单参数对象
fmt.Fprintf(w,"这是一个开始")
}
func main(){
http.HandleFunc("/",myWeb)
fmt.Println("服务器即将开启,访问地址 http://localhost:830")
err := http.ListenAndServe(":830",nil)
if err !=nil {
fmt.Println("服务器开启错误:",err)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
动态响应数据给访客,Go http HTML模版+数据绑定
package main
import (
"fmt"
"net/http"
"text/template"
)
func myWeb(w http.ResponseWriter,r *http.Request){
//t:=template.New("index")
//t.Parse("<div id='templateTextDiv'>Hi,{{.name}},{{.someStr}}</div>")
//将上两句注释掉,用下面一句
t,_:=template.ParseFiles("./html/template/index.html")
data := map[string]string{
"name": "wangwei",
"someStr":"这是一个开始",
}
t.Execute(w,data)
}
func main(){
http.HandleFunc("/",myWeb)
fmt.Println("服务器即将开启,访问地址 http://localhost:830")
err := http.ListenAndServe(":830",nil)
if err !=nil {
fmt.Println("服务器开启错误:",err)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>Hello {{.name}}</div>
<div>{{.someStr}}</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
加载js
alert("Javascript running...");
- 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="/js/index.js"></script>
<div>Hello {{.name}}</div>
<div>{{.someStr}}</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
package main
import (
"fmt"
"net/http"
"text/template"
)
func myWeb(w http.ResponseWriter,r *http.Request){
//t:=template.New("index")
//t.Parse("<div id='templateTextDiv'>Hi,{{.name}},{{.someStr}}</div>")
//调用t.Parse函数解析字符串模版
//将上两句注释掉,用下面一句
t,_:=template.ParseFiles("./html/template/index.html")
data := map[string]string{
"name": "wangwei",
"someStr":"这是一个开始",
}
t.Execute(w,data)
}
func main(){
http.HandleFunc("/",myWeb)
指定文件服务路径
//staticHandle := http.FileServer(http.Dir("./html/static"))
将/js/路径的请求匹配到件服务路径:./static/js
//http.Handle("/js/",staticHandle)
//也可以写成一句,更容易理解
//浏览器访问/js/ 将会以静态文件形式访问目录 ./static/js
http.Handle("/js/", http.FileServer(http.Dir("./html/static"))) //对应到 ./static/js
http.Handle("/css/", http.FileServer(http.Dir("./html/static"))) //对应到 ./static/css
http.Handle("/img/", http.FileServer(http.Dir("./html/static"))) //对应到 ./static/img
fmt.Println("服务器即将开启,访问地址 http://localhost:830")
err := http.ListenAndServe(":830",nil)
if err !=nil {
fmt.Println("服务器开启错误:",err)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
目录结构
加载css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
<script src="/js/index.js"></script>
<div class="divA">Hello {{.name}}</div>
<div class="divB">{{.someStr}}</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
.Margin8px{
margin: 8px;
}
.divA{
display: flex;
/*background-color: #7D91DB;*/
width: 100%;
text-align: right;
margin-bottom: 8px;
}
.divA div {
flex: 1;
}
.divB{
display: flex;
background-color: #A5D285;
}
.divB div {
flex: 1;
border-left: 1px solid white;
height: 800px;
text-align: center;
line-height: 41px;
}
.divB h2{
color: #1E7DBB;
}
.buttonB{
color: #1E7DBB;
width: 80%;
height: 40px;
font-size: 16px;
}
.h2B{
border-bottom : 1px solid white;
}
.divZ{
background-color: #7D91DB;
width: 100%;
text-align: right;
}
.weiHeight{
font-size: 18px;
font-family: "方正准圆简体";
line-height: 30px;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫