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

如何重构指针重复使用的Golang代码库,go,Go,重构如下重复代码库的最佳方法是什么?我在Golang上查看了一些不同的方法,但没能很快找到有用的东西。我还使用了gorestfulpackage和Swagger func (api ApiResource) registerLostLogin(container *restful.Container) { ws := new(restful.WebService) ws. Path("/lostlogin"). Doc("Lost login").

重构如下重复代码库的最佳方法是什么?我在Golang上查看了一些不同的方法,但没能很快找到有用的东西。我还使用了
gorestful
package和Swagger

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的接收者使用尊重运算符和指针,会更好吗?

您当然可以将它们包装起来。。也许是这样的:

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)
    }
}
然后调用它,传入所有方法:

api := ApiResource{map[string]intapi.OxiResp{}}

registerHandlers(api.registerLostLogin, 
                 api.registerAccount)
 func (api ApiResource) registerAccount(container *restful.Container, ws *restful.WebService)
然后,您只需要在方法中接受
WebService
实例:

api := ApiResource{map[string]intapi.OxiResp{}}

registerHandlers(api.registerLostLogin, 
                 api.registerAccount)
 func (api ApiResource) registerAccount(container *restful.Container, ws *restful.WebService)

至少跨方法调用重新使用
WebService
实例。不过代码要多一点。

您如何调用这些?按顺序?我会用另一个函数来包装它,并将它传入。。。如果没有,包中是否有一个可以用自己的类型包装并实现的接口?@SimonWhitehead这些接口是按顺序调用的。我只是想知道是否可以用
函数文本
和函数包装器包装,但我认为你的建议也有意义。我只是想知道是否有任何具体的方法来实现这一点:)请原谅我的向后变量方法参数。我用post
..
而不是pre
..
。现在修好了,太好了!非常感谢西蒙!:)没问题。快乐地鼠!:)Golang真是太棒了。我不知道为什么人们想用Nodejs来代替!另一件事,我是否需要在接收器中传入尊重操作符
*apirource
?这比抄袭好吗?