2015-04-01 43 views
-1

你好我與谷歌golang重置服務工作我有POST方法 問題,當我嘗試運行這段代碼是展後數據不確定Golang重置服務post方法後的數據不確定

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 

請幫忙解決這個問題
謝謝

+1

IMO的錯誤信息是非常* *清晰。在第28行你有'...(發佈用戶)',它試圖使用'用戶'作爲一種類型。您尚未定義任何此類型。在第29行,你有'fmt.Println(用戶)'這是試圖使用'用戶'作爲一個變量。你還沒有定義任何這樣的變量。也許你需要(重新)學習[語言規範](https://golang.org/reg/spec)或(重新)參加[Go tour](http://tour.golang.org/)或使用您可以在[Go tag wiki](http://stackoverflow.com/tags/go/info)上找到其他一些基本參考資料。 – 2015-04-01 05:28:32

+0

是的,我知道 我使用本教程https://code.google.com/p/gorest/ 你能告訴我方式來訪問這篇文章數據 – smartechno 2015-04-01 06:00:02

回答

-1
 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 
} 
0
func (serv HelloService) Posted(posted User) { 
    fmt.Println(User) 
} 

應該是

func (serv HelloService) Posted(posted User) { 
    fmt.Println(posted) 
} 

張貼的方法需要與張貼和類型的用戶的名稱的參數。

你應該打印的實際paramater,而不是它的類型 - 就像你在這裏

func (serv HelloService) SayHello(name string) string { 
    return "Hello " + name 
} 
相關問題