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的更多相关文章
随机推荐
- [MVC]在练习MusicStore过程中问题实录
1,问题描述:MVC在添加基于框架的控制器时,出现无法检索xxx的元数据 参考目录:http://www.cnblogs.com/0banana0/p/4050793.html#undefined 解 ...
- c++值传递和引用及指针传递区别
以下程序各有何问题? ***************************************************************************************** ...
- ssm依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- PTA 02-线性结构4 Pop Sequence (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence (25分) Given a stack wh ...
- nginx1.6.3
Nginx1.6.3安装配置 安装时关闭防火墙和selinuxservice iptables stopsed -i "s/selinux=enabled/selinux=disable/g ...
- DELL IDRAC API接口开发文档翻译及client模块
今天和DELL官网要了一份关于服务器IDRAC 版本7/8 的API开发文档,花了一天的时间,进行了翻译,不一定全部准确,但对于英语不好的人会有所帮助.也不用重复造轮子了.下载链接: IDRAC AP ...
- SGU 106 The equation【扩展欧几里得】
先放一张搞笑图.. 我一直wa2,这位不认识的大神一直wa9...这样搞笑的局面持续了一个晚上...最后各wa了10发才A... 题目链接: http://acm.hust.edu.cn/vjudge ...
- 继续畅通工程--hdu1879(最小生成树 模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题 以为就是n行 后来一看是n*(n-1)/2行 是输入错误 真是够够的 #incl ...