The issue with doing it that way is that you are going to encode to json, decode to object, filter fields, then encode back to json - not very efficient.
You are better to write your own middleware that takes the object response, and encodes the selected fields, then writes to the ResonseWriter. > On Feb 6, 2019, at 7:56 AM, http403 <hartman...@gmail.com> wrote: > > Hi, > > I'm learning Go and start a Restful TO-DO app project for practicing. In the > process, I would like to add the Field Selector. I had searching on google > last few hours for a way to intercept the response and strip out the > unnecessary parts. I tried out some of them which either doesn't work at all > to having error with other functions. Methods I tried include > custom ResponseWriter struct which isn't compaible with HandlerFunc(). It > said it can't take a custom ResponseWriter but only http.ResponseWriter > wrapper style described in The http.Handler wrapper technique in #golang > UPDATED which doesn't work at all > middleware style showed in Writing middleware in #golang and how Go makes it > so much fun. which Handler() complain my other handler (the route logic) > doesn't have ServeHTTP() implemented. > I know I can just strip the JSON before writing to ResponseWriter but I want > it to apply automatically for every request which I doesn't need to add a > piece/line of code every time before writing anything to ResponseWriter. > > Please sent help. > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.