GopherCon 2014

Desarrollando servicios web con Go

Richard Crowley  · 

Presentación

HTML (pincha para descargar)

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

so there's a lot of you so despite the fact that I'm using the go present tool I made the fonts bigger so like you said my name is Richard and this is how you get a hold of me on the Internet this is actually my vacation because Monday I start working

at slack on the communications tool you guys should all be using at work but I'm not here to talk about that I'm here to talk about Web Services and go and I thought it would probably be good to start by getting a common definition for all this so

I hope you agree but at least for the purposes of this talk a web service we're gonna think of as a set of data and some operations that we can perform on that data and it's going to be accessed over the network using the HTTP protocol otherwise it's

not a web anything and and the the requests and responses that we make and that we receive are going to be structured and understandable by machines so they can take action on our behalf otherwise it's a it's a web application not really a web service

so and go I thought we'd start here Rob handled every hello world example ever so we're gonna skip straight to hello dub-dub-dub where our main function sets up a handler for slash the route URL and and it gives it this function literal with the signature

response writer and request pointer from the HTTP package and all it does is it takes that that response writer and sends hello dub-dub-dub to it so right away we can see that that implements the i/o writer interface which is important because we can pass

it to func print line and and so this listens on ad ad and is utterly boring in every other way I want to focus in on this this one function signature though this response writer and request pointer it's a it's a destination and source kind of thing

which you see all over the place in IO copy and well IO copy is at least good enough and by itself it doesn't look terribly special and when it's in a function literal it really doesn't look special but if you put it in an interface it starts to

take a little more meaning so this is the handler interface from the standard library from the HTTP package and sure enough there's one method and this is important because these are the kind of interfaces that we can really reuse one method called serve

HTTP that has that same signature response writer request a pointer this is the absolute lowest common denominator for a go web service for a go web anything and the rest of this talk is really to encourage you to embrace that too to use it to its fullest

possible extent and and so the first step to that is rewriting our dumb hello dub-dub-dub example to use the actual interface so I named the type it's an empty type it's a boring type and I defined the serve HTTP method with the same basic structure

as it had before and I've added a couple of things just to show the the full extent of the interface we set the content type header this is plain text so it's a text plane and we're explicitly responding 200 now that's great we're still

saying hello dub-dub-dub and and that's not a web service because that's not really very structured data that's that's just bytes so let's rewrite this thing to respond with Jason because that's of course that's basically what we

do these days so the content type of course changes now it's application Jason still 200 because everything's ok and now we're gonna be we're gonna build up this jason encoder shenanigans and an adjacent encoder wraps around any old writer

so of course our response writer will do the trick and now we get to encode some arbitrary object like my response which i just made up the problem is that you can have errors in this scenario that and and they might not be very common errors but if goes taught

us anything it's that errors should be handled front and center as if we expect them to happen all the time so that makes this a lot more verbose it makes it something that you don't really want to do for every single route in your in your web application

or your web service and so we have a problem but we also have another problem which is introduced by this error because if I rewrite the the basic you know just hello dub-dub-dub handler to be more like an error at least in the HTTP sense it doesn't actually

look any different than what we had before and errors should be noticeable they should be they should be unique they should have a code path all their own and and then go they should have an actual error value right so this is a bit dissatisfying to me and

this is something that in combination with the verbosity and the annoyance of having to deal with the JSON encoder means we need to within the context of this handler step up our abstraction just a little bit so the errors that we want to get to or something

[ ... ]

Nota: se han omitido las otras 2.418 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.