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") + " httpport" + beego.AppConfig.String("httpport") + " runmode:" + beego.AppConfig.String("runmode")) //读取的是conf里面的app.conf文件里面的内容 // hp := strconv.Itoa(beego.HttpPort) // this.Ctx.WriteString("appname:" + beego.AppName + // " httpport" + hp + // " runmode:" + 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")) } }