我很难理解go generate。我也发现几乎没有任何与go generate.
请go generate在以下示例中进行说明:
package main
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// --- Address
type Address struct {
Id bson.ObjectId `bson:"_id,omitempty"`
AccountId string `bson:"account_id"`
Name string `bson:"name"`
StreetAddress string `bson:"streetaddress"`
Town string `bson:"town"`
Country string `bson:"country"`
}
// --- AddressHandler
type AddressHandler struct {
MS *mgo.Session
}
func NewAddressHandler(ms *mgo.Session) *AddressHandler {
return &AddressHandler{MS: ms.Clone()}
}
func (h *AddressHandler) Close() {
h.MS.Close()
}
// Add
type AddAddressInput struct {
Address *Address
}
type AddAddressOutput struct {
Error error
}
func (h *AddressHandler) AddAddress(in *AddAddressInput, out *AddAddressOutput) {
ms := h.MS.Copy()
defer ms.Close()
c := ms.DB("").C("address")
out.Error = c.Insert(in.Address)
}
// Remove
type RemoveAddressInput struct {
AddressId string
}
type RemoveAddressOutput struct {
Error error
}
func (h *AddressHandler) RemoveAddress(in *RemoveAddressInput, out *RemoveAddressOutput) {
ms := h.MS.Copy()
defer ms.Close()
c := ms.DB("").C("address")
out.Error = c.RemoveId(bson.ObjectIdHex(in.AddressId))
}
// Update
type UpdateAddressInput struct {
Address *Address
}
type UpdateAddressOutput struct {
Error error
}
func (h *AddressHandler) UpdateAddress(in *UpdateAddressInput, out *UpdateAddressOutput) {
ms := h.MS.Copy()
defer ms.Close()
c := ms.DB("").C("address")
out.Error = c.UpdateId(in.Address.AccountId)
}
// GetAllByAccount
type GetAddressInput struct {
AccountId string
}
type GetAddressOutput struct {
Address []*Address
Error error
}
我想创建这个尚未模板代码的几乎副本。
PIPIONE
相关分类