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的更多相关文章

随机推荐

  1. SHELL二十篇(读书笔记)

    一.文件安全与权限 1.系统默认情况下建立文件与目录的权限: 系统默认情况下建立文件与目录的权限是: #vi /etc/bashrc 可以看到一般用户是002  root用户是022 说明:一般用户默 ...

  2. luogu4168 [Violet]蒲公英

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  3. Java基础学习总结(94)——Java线程再学习

    Java线程有哪些不太为人所知的技巧与用法? 萝卜白菜各有所爱.像我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是 ...

  4. C#窗体学生成绩管理系统

    c#学生成绩管理系统 实现用户登录.注册 所有成绩查询.个人成绩查询 管理员审核.添加.删除用户 项目源码GIT:https://github.com/soulsjie/StuScoreMa.git

  5. zoj 1949 Error Correction

    Error Correction Time Limit: 2 Seconds      Memory Limit: 65536 KB A boolean matrix has the parity p ...

  6. 【2018 Multi-University Training Contest 5】

    01: 02:https://www.cnblogs.com/myx12345/p/9436953.html 03: 04: 05:https://www.cnblogs.com/myx12345/p ...

  7. Drainage Ditches--hdu1532(网络流 模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...

  8. 洛谷—— P1098 字符串的展开

    https://www.luogu.org/problem/show?pid=1098 题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类 ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  10. mybatis几种开发方式

    mybatis是比较轻巧的半自动化的CRM框架,它有几种开发方式,现今张列于此: 一.注解方式:在接口方法上面写SQL语句,有点类似springdataJPA 的query sql 语句 范例 @se ...