beego6
package main //beego使用的是go语言原生的模版 import (
//_ "beego1/routers" //默认controll文件夹里面的控制器
"github.com/astaxie/beego"
//"strconv"
) type HomeController struct {
beego.Controller
} func (this *HomeController) Get() {
this.Ctx.WriteString("appname::::::" + beego.AppConfig.String("appname") +
"\nhttpport" + beego.AppConfig.String("httpport") +
"\nrunmode:" + beego.AppConfig.String("runmode")) //读取的是conf里面的app.conf文件里面的内容 // hp := strconv.Itoa(beego.HttpPort)
// this.Ctx.WriteString("appname:" + beego.AppName +
// "\nhttpport" + hp +
// "\nrunmode:" + beego.RunMode) //读取的是conf里面的app.conf文件里面的内容 //打印
beego.Trace("trace")
beego.Info("info")
beego.Debug("debug")
beego.Warn("warn")
beego.Error("error") } func main() {
beego.Router("/", &HomeController{})
beego.Run()
}
go原生读取cookie
package main import (
"io"
"net/http"
"strings"
) func mian() {
http.HandleFunc("/", cookie)
http.ListenAndServe(":8080", nil)
} func cookie1(w http.ResponseWriter, r *http.Request) {
ck := &http.Cookie{
Name: "mycookie",
Value: "hello",
Path: "/", //路径根目录
Domain: "localhosst", //域名
MaxAge: 120,
}
http.SetCookie(w, ck) //设置cookie,ck是cookie
ck2, err := r.Cookie("mycookie") //读取cookie
if err != nil {
io.WriteString(w, err.Error())
return
}
io.WriteString(w, ck2.Value)
} func cookie(w http.ResponseWriter, r *http.Request) {
ck := &http.Cookie{
Name: "mycookie",
Value: "hellowwww",
Path: "/", //路径根目录
Domain: "localhosst", //域名
MaxAge: 120,
}
w.Header().Set("Set-Cookie", ck.String()) //通过Header设置cookie
w.Header().Set("Set-Cookie", strings.Replace(ck.String(), " ", "%20", -1)) //除去空格
ck2, err := r.Cookie("mycookie") //读取cookie
if err != nil {
io.WriteString(w, err.Error())
return
}
io.WriteString(w, ck2.Value)
}
go原生解析表单 package main //直接使用go模仿beego解析表单,在src下建立一个文件夹test,
//test里面就放一个main.go。通过git Bash进入到该目录,
//go run main.go
import (
"fmt"
//"io"
"html/template"
"net/http"
) func main() {
http.HandleFunc("/", Hey)
http.ListenAndServe(":8080", nil)
} const tpl = `
<html>
<head>
<title>hey</title>
</head>
<body>
<form method="POST" action="/">
name: <input name="name" id="name"/>
pwd: <input name="pwd" id="pwd"/>
<input type="submit">ok</input>
</form>
</body>
</html>
` func Hey(w http.ResponseWriter, r *http.Request) {
//前面不加*号是接口,后面加*号是struct,要传地址,
//只有这种签名的函数才能够注册为handler
// fmt.Println("llllll")
// io.WriteString("ssssssssfffffff)
if r.Method == "GET" { //get请求的时候返回html
t := template.New("hey")
t.Parse(tpl)
t.Execute(w, nil)
} else { //post请求的时候解析form
fmt.Println(r.FormValue("name"))
}
}
beego6的更多相关文章
随机推荐
- Laravel(4.2)-->whereHas/ whereDoesntHave
在开发过程中,有时间需要用 wherehas 联合查询 出想要的结果,但是有的时候想搜索出不在关联表中出现的数据 whereDoesntHave(例:搜索出开卡的用户和没有开卡的用户)if($is_o ...
- hibernate中时间比较的小笔记
// 开单时间 if (!"".equals(startDate) && startDate != null) { queryCondition = queryCo ...
- luogu3157 [CQOI2011]动态逆序对
先算出一个点前头比它大和后头比它小的数量. 每次删点就扔进一个主席树里头,防止造成重复删答案. #include <iostream> #include <cstring> # ...
- js如何获取点击<li>标签里的值
- Leetcode 313.超级丑数
超级丑数 编写一段程序来查找第n个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13,19] ...
- [codeforces538E]Demiurges Play Again
[codeforces538E]Demiurges Play Again 试题描述 Demiurges Shambambukli and Mazukta love to watch the games ...
- [luoguP2216] [HAOI2007]理想的正方形(二维单调队列)
传送门 1.先弄个单调队列求出每一行的区间为n的最大值最小值. 2.然后再搞个单调队列求1所求出的结果的区间为n的最大值最小值 3.最后扫一遍就行 懒得画图,自己体会吧. ——代码 #include ...
- 【dp】codeforces C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...
- SpringBoot自定义Filter
SpringBoot自定义Filter SpringBoot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,当然我们可以自定 义F ...
- socket相关
socket层 图示,没有找到socket,那么socket层在哪儿呢? 看图: socket是什么 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Sock ...