Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building REST API with GoLang

Burak Aydın
November 14, 2015

Building REST API with GoLang

Devfest'15 Ankara

Burak Aydın

November 14, 2015
Tweet

More Decks by Burak Aydın

Other Decks in Technology

Transcript

  1. Why Go Micro services hype Easy deployment Performance blog.parse.com/learn/how-we-moved-our-api-from-ruby-to-go-and-saved-our- sanity/

    (http://blog.parse.com/learn/how-we-moved-our-api-from-ruby-to-go-and-saved-our-sanity/)
  2. Differs Go compiles to machine code. Function values, closures Strings

    UTF-8 Built-in concurreny Go has great tools!
  3. Go Mentality Do something very well, but don't do so

    much. No news is good news. Perfect?
  4. Hello Hello.java p u b l i c c l

    a s s M a i n { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { S y s t e m . o u t . p r i n t l n ( " H e l l o , w o r l d ! " ) ; } } Hello.go f u n c m a i n ( ) { f m t . P r i n t l n ( " H e l l o , G o " ) }
  5. Json in Go p a c k a g e

    m a i n i m p o r t ( " f m t " " e n c o d i n g / j s o n " ) t y p e U s e r s t r u c t { i d i n t n a m e s t r i n g } f u n c m a i n ( ) { d a t a : = [ ] b y t e ( ` { " i d " : 1 2 3 , " n a m e " : " j i l l " } ` ) u s e r : = U s e r { } e r r : = j s o n . U n m a r s h a l ( d a t a , & u s e r ) i f e r r ! = n i l { p a n i c ( e r r ) } f m t . P r i n t f ( " H e l l o % s " , u s e r . n a m e ) } Run
  6. Json in Go p a c k a g e

    m a i n i m p o r t ( " f m t " " e n c o d i n g / j s o n " ) t y p e U s e r s t r u c t { I d i n t ` j s o n : " i d " ` N a m e s t r i n g ` j s o n : " n a m e " ` } f u n c m a i n ( ) { d a t a : = [ ] b y t e ( ` { " i d " : 1 2 3 , " n a m e " : " j i l l " } ` ) u s e r : = U s e r { } e r r : = j s o n . U n m a r s h a l ( d a t a , & u s e r ) i f e r r ! = n i l { p a n i c ( e r r ) } f m t . P r i n t f ( " H e l l o % s " , u s e r . N a m e ) } Run
  7. Webserver p a c k a g e m a

    i n i m p o r t ( " f m t " " l o g " " n e t / h t t p " ) f u n c m a i n ( ) { h t t p . H a n d l e F u n c ( " / h e l l o " , h a n d l e H e l l o ) f m t . P r i n t l n ( " s e r v i n g o n h t t p : / / l o c a l h o s t : 8 0 8 0 / h e l l o " ) l o g . F a t a l ( h t t p . L i s t e n A n d S e r v e ( " l o c a l h o s t : 8 0 8 0 " , n i l ) ) } f u n c h a n d l e H e l l o ( w h t t p . R e s p o n s e W r i t e r , r e q * h t t p . R e q u e s t ) { l o g . P r i n t l n ( " s e r v i n g " , r e q . U R L ) f m t . F p r i n t l n ( w , " H e l l o , 世界! " ) } Run
  8. Mux ServeMux is an HTTP request multiplexer. p a c

    k a g e m a i n i m p o r t ( " n e t / h t t p " " l o g " " f m t " ) f u n c m a i n ( ) { m u x : = h t t p . N e w S e r v e M u x ( ) m u x . H a n d l e F u n c ( " / h e l l o " , h a n d l e r ) l o g . F a t a l ( h t t p . L i s t e n A n d S e r v e ( " : 8 0 8 0 " , m u x ) ) } f u n c h a n d l e r ( w h t t p . R e s p o n s e W r i t e r , r * h t t p . R e q u e s t ) { f m t . F p r i n t l n ( w , " H e l l o M u x ! " ) } Run
  9. Needs for REST API Routing Middleware Authentication CORS Hot code

    reloading Easy db access Context sharing Logging