building scalable backends with go
TRANSCRIPT
BuildingScalableBackendsWithGo
ShijuVarghese
AboutMe
• ConsultingSolutionsArchitect• FocusedonGoandGoogleCloud
@shijucv|https://medium.com/@shijuvar
Agenda
• IntroductiontoGo• Goasalanguageforbuildingscalablebackendsystems• Demo-RESTfulAPIwithGo
GoasaLanguageforBuildingScalableBackendSystems
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Gocanbedescribedinthreewords:Simple,MinimalandPragmatic.
» MinimalisticlanguagewithPragmaticDesign» AStaticTypeLanguagewithHighProductivity» ConcurrencyisaBuilt-inFeature» GoCompilesProgramsQuickly» GoisaGeneral-PurposeLanguage» GoMobileProject
IntroducingGo
GoEcosystem
» Golanguage» Golibraries» Gotooling
WhyGo?
» ConcurrencyandCommunicatingSequentialProcesses(CSP)
» ModernC» PragmatismandDeveloperproductivity» GoCommunity» Gosuccessstories-Docker,Kubernetes,CoreOSandsuccessfulimplementationsofMicroservices
» LanguagefortheeraofCloud,MicroservicesandContainers
BuildingHTTPServers
net/httpPackage
» ProvidesHTTPclientandserverimplementations» AllowsyoutobuildHTTPserversinGo» Providescomposabilityandextensibility
ProcessingHTTPRequests
» ServeMux-HTTPrequestmultiplexer» Handler-ServeHTTPrequests
http.HandlerInterface
type Handler interface { ServeHTTP(ResponseWriter, *Request) }
» ServeHTTPrequests.» HandlersareresponsibleforwritingheadersandbodiesintoHTTPresponses.
packagemain
import("fmt""net/http")
funchello(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"Helloworld!")}
funcmain(){http.HandleFunc("/",hello)http.ListenAndServe(":8000",nil)}
AnHTTPServer
HTTPMiddleware
» Pluggableandself-containedpieceofcodethatwrapswebapplicationhandlers.
» Componentsthatworkasanotherlayerintherequesthandlingcycle,whichcanexecutesomelogicbeforeorafterexecutingapplicationhandlers.
» Greatforimplementingcross-cuttingconcerns:Authentication,authorization,caching,loggingetc.
funcloggingHandler(nexthttp.Handler)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){start:=time.Now()log.Printf("Started%s%s",r.Method,r.URL.Path)next.ServeHTTP(w,r)log.Printf("Completed%sin%v",r.URL.Path,time.Since(start))})}
//ApplyingMiddlewarehttp.Handle("/",loggingHandler(indexHandler))http.Handle("/about",loggingHandler(aboutHandler))
Demo-RESTAPIwithGoandMongoDB
Source:https://github.com/shijuvar/go-web/tree/master/taskmanager
» GoisagreatstackforbuildingscalablebackendsystemsandMicroservices.
» GoisagreatstackforbuildingRESTfulAPIs.
KeyTakeaways