gpt4 book ai didi

google-app-engine - Go:如何在数据存储中获取 "put"r.FormValue 以及如何从数据存储中获取 "get"?

转载 作者:数据小太阳 更新时间:2023-10-29 03:42:38 26 4
gpt4 key购买 nike

例子:
1)通过模板方法呈现登录页面。例如:这是 index.html

{{ define "title" }}Guestbook{{ end }}
{{ define "content" }}
<form action="/login" method="post">
<div><label>UserName : </label><input name="username" type="text" /></div>
<div><label>Password : </label><input name="password" type="password" /></div>
<div><input type="submit" value="login"></div>
</form>
{{ end }}

2) hello.go 文件:

package main 

import (
"fmt"
"html/template"
"net/http"
)

var index = template.Must(template.ParseFiles(
"templates/base.html",
"templates/index.html",
))
//UserLogin struct is created
type UserLogin struct{
UserName string
PassWord string
}

func handler(w http.ResponseWriter, r *http.Request) {
index.Execute(w, nil)
}


func login(w http.ResponseWriter, r *http.Request) {
remPartOfURL := r.URL.Path[len("/login/"):]
if r.Method == "POST" {
http.Error(w, fmt.Sprintf("First Name: %s", r.FormValue("username")), http.StatusOK)
http.Error(w, fmt.Sprintf("Password: %s", r.FormValue("password")), http.StatusOK)
}
}

func init() {
http.HandleFunc("/", handler)
http.HandleFunc("/login/", login)
}

在 login() 的这个例子中:能够打印 r.FormValue("username") 和 r.FormValue("password") 但如何“放入”数据存储以及如何从数据存储“获取”。

最佳答案

引用:https://github.com/stephenlewis/eveningwithgo/blob/master/datastore/datastore/datastore.go

答案:

主要包

import (
"fmt"
"html/template"
"net/http"
"appengine"
"appengine/datastore"
)

var index = template.Must(template.ParseFiles(
"templates/base.html",
"templates/index.html",
))

type cUserLogin struct{
UserName string
PassWord string
}

func handler(w http.ResponseWriter, r *http.Request) {
index.Execute(w, nil)
fmt.Fprint(w, "handler : ", "\n")
c := appengine.NewContext(r)
q := datastore.NewQuery("cUserLogin")
w.Header().Add("Content-Type", "text/plain")
for t := q.Run(c); ; {
var getuser cUserLogin
key, err := t.Next(&getuser)
if err == datastore.Done {
break
}
fmt.Fprintf(w, "%v: %s %s\n", key, getuser.UserName, getuser.PassWord)
}

}

func login(w http.ResponseWriter, r *http.Request) {
remPartOfURL := r.URL.Path[len("/login/"):]
c := appengine.NewContext(r)
if r.Method == "POST" {
http.Error(w, fmt.Sprintf("First Name: %s", r.FormValue("username")), http.StatusOK)
http.Error(w, fmt.Sprintf("Password: %s", r.FormValue("password")), http.StatusOK)
g := cUserLogin{
UserName: r.FormValue("username"),
PassWord: r.FormValue("password"),
}
key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "cUserLogin", nil), &g)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Written with key %v\n", key)
}

fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}


func init() {
http.HandleFunc("/", handler)
http.HandleFunc("/login/", login)
}

关于google-app-engine - Go:如何在数据存储中获取 "put"r.FormValue 以及如何从数据存储中获取 "get"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com