last day (14 days later) » 

1:05 PM
1
A: How to refactor repeated Golang codebase usage of pointer

Simon WhiteheadYou can certainly wrap them up.. perhaps something like this: type regisration func(container *restful.Container, ws *restul.WebService) func registerHandlers(handlers ...registration) { c := restful.NewContainer() ws := new(restful.WebService) for _, h := range handlers { ...

 
Please excuse my backwards variadic method arguments. I had post ... instead of pre .... Fixed now.
 
Great! Thanks a lot Simon! :)
 
No problem at all. Happy Gophering! :)
 
Golang is awesome. I don't know why people want to use Nodejs instead!
Another thing, do I need to pass in deference operator *ApiResource in the receiver instead? Is it better than copying?
 
I can't really speak to that given that I can't see what ApiResource is/does. Having said that, it sounds like its big enough to warrant a pointer receiver, as it sounds like it manages its own state. So, I would say "yes" to using *ApiResource.
 
1:05 PM
Great thanks. By the way I'm getting undefined: registration error. Do I need to define registration func?
 
Yes.. see the very first line of code in my answer? You need that.
 
Yes. I already have it in there above registerHandlers
 
hi there
 
Can you show me the code you have?
also.. is this all in one package?
 
1:06 PM
sure
type regisration func(container *restful.Container, ws *restful.WebService)

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

for _, h := range handlers {
h(c, 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{}}

registerHandlers(
api.registerLogin,
api.registerAccount,
api.registerLostLogin,
api.registerWallet)

config := swagger.Config{
WebServices: wsContainer.RegisteredWebServices(), // you control what services are visible
WebServicesUrl: "http://localhost:8080",
 
ahhh
typo
sorry
 
there is a missing "t" in the type definition
 
I should have picked it up...sorry my fault!!!
 
no problem at all :)
 
1:07 PM
tx!
 
my answer should be correct in the first place! I didn't compile it.. I just typed it.
I always mis-type stuff
 
haha human error :)
 
updated my answer :)
all working now?
 
I have another error but I think it could be related to the go-restful package
I'll figure it out some how :)
 
no problem
enjoy! :)
 
1:09 PM
thank you
:)
 

last day (14 days later) »