首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重构指针的重复Golang代码库用法

如何重构指针的重复Golang代码库用法
EN

Stack Overflow用户
提问于 2014-09-21 10:30:39
回答 1查看 370关注 0票数 2

以下重构重复代码库的最佳方法是什么?我在Golang上看了几种不同的方法,但没能很快找到有用的东西。我还使用了Swagger的go-restful

代码语言:javascript
复制
func (api ApiResource) registerLostLogin(container *restful.Container) {
    ws := new(restful.WebService)
    ws.
        Path("/lostlogin").
        Doc("Lost login").
        Consumes(restful.MIME_JSON, restful.MIME_XML).
        Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well

    ws.Route(ws.POST("").To(api.authenticate).
        Doc("Performs lost login actions").
        Operation("lostlogin").
        Reads(LostLogin{})) // from the request

    container.Add(ws)
}

func (api ApiResource) registerAccount(container *restful.Container) {
    ws := new(restful.WebService)
    ws.
        Path("/account").
        Doc("Account calls").
        Consumes(restful.MIME_JSON, restful.MIME_XML).
        Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well

    ws.Route(ws.POST("").To(api.authenticate).
        Doc("Register calls").
        Operation("register").
        Reads(Account{}))

    ws.Route(ws.PUT("").To(api.authenticate).
        Doc("Modify user details").
        Operation("modifyUserDetails").
        Reads(Account{}))

    ws.Route(ws.PUT("security").To(api.authenticate).
        Doc("Modify security question").
        Operation("modifySeucirtyQuestion").
        Reads(Account{}))

    ws.Route(ws.PUT("limit").To(api.authenticate).
        Doc("Modify limit").
        Operation("modifyLimit").
        Reads(Account{}))

    ws.Route(ws.PUT("password").To(api.authenticate).
        Doc("Modify password").
        Operation("modifyPassword").
        Reads(Account{}))

    container.Add(ws)
}

func main() {
    // to see what happens in the package, uncomment the following
    restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))

    wsContainer := restful.NewContainer()
    api := ApiResource{map[string]intapi.OxiResp{}}
    api.registerLogin(wsContainer)
    api.registerAccount(wsContainer)
    api.registerLostLogin(wsContainer)
    api.registerWallet(wsContainer)

    config := swagger.Config{
        WebServices:     wsContainer.RegisteredWebServices(), // you control what services are visible
        WebServicesUrl:  "http://localhost:8080",
        ApiPath:         "/apidocs.json",
        SwaggerPath:     "/apidocs/",
        SwaggerFilePath: "/Users/aa/IdeaProjects/go_projects/src/test1/src/swagger-ui/dist",
        DisableCORS:     false}
    swagger.RegisterSwaggerService(config, wsContainer)

    log.Printf("start listening on localhost:8080")
    server := &http.Server{Addr: ":8080", Handler: wsContainer}
    log.Fatal(server.ListenAndServe())
}

我主要关心的是复制所有的ws被跨。如果我对带有ApiResource的接收方使用尊重操作符和指针,会更好吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-21 12:06:27

你当然可以把它们包起来。也许是这样的:

代码语言:javascript
复制
type registration func(container *restful.Container, ws *restul.WebService)

func registerHandlers(handlers ...registration) {
    c := restful.NewContainer()
    ws := new(restful.WebService)

    for _, h := range handlers {
        h(c, ws)
    }
}

然后调用它,传入所有的方法:

代码语言:javascript
复制
api := ApiResource{map[string]intapi.OxiResp{}}

registerHandlers(api.registerLostLogin, 
                 api.registerAccount)

然后,只需在方法中接受WebService实例:

代码语言:javascript
复制
 func (api ApiResource) registerAccount(container *restful.Container, ws *restful.WebService)

这至少会在方法调用中重用WebService实例。不过,代码要多一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25958271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档