Post Golang重置服务岗位方法岗位数据未定义
嗨,我正在使用谷歌golang重置服务,我对post方法有问题 当我尝试运行此代码时,它显示未定义的post数据Post Golang重置服务岗位方法岗位数据未定义,post,go,gorest,Post,Go,Gorest,嗨,我正在使用谷歌golang重置服务,我对post方法有问题 当我尝试运行此代码时,它显示未定义的post数据 package main import ( "code.google.com/p/gorest" "fmt" "net/http" ) type Invitation struct { User string } //Service Definition type HelloService struct { gorest.Rest
package main
import (
"code.google.com/p/gorest"
"fmt"
"net/http"
)
type Invitation struct {
User string
}
//Service Definition
type HelloService struct {
gorest.RestService
//gorest.RestService `root:"/tutorial/"`
helloWorld gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
sayHello gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
posted gorest.EndPoint `method:"POST" path:"/post/" postdata:"User" `
}
func main() {
gorest.RegisterService(new(HelloService)) //Register our service
http.Handle("/", gorest.Handle())
http.ListenAndServe(":8787", nil)
}
func (serv HelloService) Posted(posted User) {
fmt.Println(User)
}
func (serv HelloService) HelloWorld() string {
return "Hello World"
}
func (serv HelloService) SayHello(name string) string {
return "Hello " + name
}
这就是我得到的错误
# command-line-arguments
./registation.go:28: undefined: User
./registation.go:29: undefined: User
请帮助解决此问题多谢各位
func (serv HelloService) Posted(posted User) {
fmt.Println(User)
}
应该是
func (serv HelloService) Posted(posted User) {
fmt.Println(posted)
}
posted方法接受一个参数,该参数的名称为posted,类型为User
您应该打印实际的参数,而不是它的类型-就像您在这里
func (serv HelloService) SayHello(name string) string {
return "Hello " + name
}
在我看来,错误信息非常清楚。在第28行,您有
…(已发布的用户)
,该用户试图将用户
用作类型。您尚未定义任何此类类型。第29行有fmt.Println(User)
,它试图使用User
作为变量。您尚未定义任何此类变量。也许你需要(重新)学习或(重新)采取或使用一些其他基本的参考,你可以在上找到。是的,我知道我正在使用本教程,你能告诉我如何访问此帖子数据吗
package main
import (
//"bytes"
"code.google.com/p/gorest"
"crypto/rand"
"crypto/tls"
//"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/mail"
"net/smtp"
)
type InvitEmail struct {
SenderEmail string
ReceiverEmail string
}
//Service Definition
type HelloService struct {
gorest.RestService
//gorest.RestService `root:"/tutorial/"`
helloWorld gorest.EndPoint `method:"GET" path:"/hello-world/" output:"string"`
sayHello gorest.EndPoint `method:"GET" path:"/hello/{name:string}" output:"string"`
userRegister gorest.EndPoint `method:"POST" path:"/UserRegister/" postdata:"InvitEmail"`
/*activate gorest.EndPoint `method:"GET" path:"/activate/{email:string}/{token:string}" output:"bool"`
userInvite gorest.EndPoint `method:"POST" path:"/UserInvite/" postdata:"InvitEmail"`*/
}
func main() {
gorest.RegisterService(new(HelloService)) //Register our service
http.Handle("/", gorest.Handle())
http.ListenAndServe(":8787", nil)
}
func (serv HelloService) UserRegister(u InvitEmail) {
fmt.Println(u.ReceiverEmail, "xxxxxxxx", u.SenderEmail)
invitationmail(u.SenderEmail, u.ReceiverEmail)
serv.ResponseBuilder().SetResponseCode(200).Write([]byte("from :" + u.SenderEmail + " to :" + u.ReceiverEmail))
}
func (serv HelloService) HelloWorld() string {
return "Hello World"
}
func (serv HelloService) SayHello(name string) string {
return "Hello " + name
}
func invitationmail(sender, receiver) {
//emailsendingpart
}