As a beginner, there seem to be so many options - the popular ones are Gin, Gorilla, HttpRouter and probably dozens of lesser-known ones.
What's the idiomatic way in 2017 to write REST APIs in Go?
评论:
nathj07:
WagwanKenobi:As a general rule stick the standard library until you know you need more. Then I would advise using the package that fulfills the specific need.
I've had a good experience utilising HTTPRouter and have numerous APIs in production with that.
Generally I tend to grab a package to solve a specific problem rather than look for an all encompassing framework.
tmornini:What kind of problems did you hit with net/http that HTTPRouter solved?
nathj07:Lack of routing on method caused me to pull in:
github.com/julienschmidt/httprouter
It's ultra-fast, has 100% test coverage, and is stable. Great package.
nathj07:Yeah, that's the one I've used. It's brilliant, does its thing and gets out of the way
metamatic:We pulled in HTTPRouter to make path patterns easier. Thing like
/resource/{id}
are simple.I'm away from my machine right now so can't pull specific examples up to easily.
daveddev:I use https://godoc.org/github.com/bouk/httprouter because it's basically httprouter, but with standard Context objects to pass parameters so it only needs the standard method signatures.
metamatic:Please consider https://github.com/codemodus/parth. It would allow you to drop the allocations and delay involved with handling path parameters. Another way to think of it is that we are already passing the path parameters, so there's not often a need to lean on special handler functions or to setup and pass a context type.
benchmark iter time/iter bytes alloc allocs --------- ---- --------- ----------- ------ BenchmarkVsCtxParthString2x-8 20000000 83.80 ns/op 0 B/op 0 allocs/op BenchmarkVsCtxParthString3x-8 10000000 125.00 ns/op 0 B/op 0 allocs/op BenchmarkVsCtxContextGetSetGet-8 1000000 1629.00 ns/op 336 B/op 5 allocs/op BenchmarkVsCtxContextGetSetGetGet-8 1000000 2044.00 ns/op 352 B/op 6 allocs/op
darxkies:Awesome, I was wondering when someone was going to build a library like that. I almost started on one myself.
I'll still use Context for my authentication middleware though.
shovelpost:I've tried many frameworks over the last 14 months. The one that I enjoyed the most so far is https://goa.design/. At the beginning, it is tricky to grasp all its functionality but it definitely pays off in the long term. It supports security and swagger out of the box and its DSL reduces the boilerplate code to a minimum.
WagwanKenobi:What's the idiomatic way in 2017 to write REST APIs in Go?
Standard library - net/http
Use slicing to get the keys and then you need zero dependencies.
nathj07:I just had a cursory look at net/http. Will I not be able to get all the parameters using ParseForm and Request.Form?
Also, the new Context thing seems very useful for JWT-authenticated routes. Right now I'm leaning towards net/http.
tsdtsdtsd:Using something like the aforementioned httprouter will reduce the amount of code you write and you know it works and is reliable. I've done the slicing thing before, about 4 years ago, and wouldn't go back now.
FrenchDonkey:I use the chi router (https://github.com/pressly/chi), very neat.
I don't know why Gorilla doesn't get more love
