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

Golang 101 for PHP Developers

Golang 101 for PHP Developers

A quick overview of the important parts, specifically focused on helping PHP developers.

Daniel Olfelt

June 07, 2017
Tweet

Other Decks in Technology

Transcript

  1. main.go p a c k a g e m a

    i n i m p o r t ( " f m t " ) f u n c m a i n ( ) { f m t . P r i n t f ( " H e l l o % s \ n " , " W o r l d " ) } $ g o r u n m a i n . g o H e l l o W o r l d $
  2. p a c k a g e m a i

    n i m p o r t " n e t / h t t p " f u n c s a y h e l l o N a m e ( 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 f ( w , " H e l l o D a n i e l ! " ) / / s e n d d a t a t o c l i e n t s i d e } f u n c m a i n ( ) { h t t p . H a n d l e F u n c ( " / " , s a y h e l l o N a m e ) / / s e t r o u t e r e r r : = h t t p . L i s t e n A n d S e r v e ( " : 9 0 0 0 " , n i l ) / / s e t l i s t e n p o r t i f e r r ! = n i l { l o g . F a t a l ( " L i s t e n A n d S e r v e : " , e r r ) } } $ g o r u n m a i n . g o L i s t e n i n g : 9 0 0 0
  3. pointers Pointers are a reference (or pointer) to the actual

    value. They do not contain the value themselves. / / i n i t i a l i z e b o o k : = & B o o k { } / / s p e c i f y f u n c C h a n g e T i t l e ( b * B o o k ) { / / M o d i f y i n g ` b ` h e r e w i l l m u t a t e i t s v a l u e b . T i t l e = " N e w T i t l e " } C h a n g e T i t l e ( b o o k ) f m t . P r i n t l n ( b o o k . T i t l e ) / / P r i n t s " N e w T i t l e "
  4. pointers values b o o k : = B o

    o k { } f u n c C h a n g e T i t l e ( b B o o k ) B o o k { / / M o d i f y i n g ` b ` w i l l n o t e x t e n d p a s t t h i s f u n c t i o n r e t u r n b } / / P o i n t e r / v a l u e c o n v e r s i o n b o o k P o i n t e r : = & B o o k { } b o o k V a l u e : = * b o o k P o i n t e r C h a n g e T i t l e ( b o o k V a l u e ) b o o k P o i n t e r A g a i n : = & b o o k V a l u e vs
  5. why pointers Pointers save memory when passing data around the

    application. When passing values, the data is duplicated. Values are not allowed to be nil, but pointers are. Go will type-check against types and pointer/value.
  6. arrays In PHP, an array can actually be a map

    or an array $ a r r = [ ] ; In Go, you need to be explicit a r r : = [ ] s t r i n g { } a r r : = m a p [ s t r i n g ] i n t e r f a c e { } { }
  7. arrays (well, actually maps) i f ( i s s

    e t ( $ _ P O S T [ ' i d ' ] ) ) { $ i d = $ _ P O S T [ ' i d ' ] ; / / D o s o m e t h i n g } vs i f i d , o k : = p o s t [ " i d " ] ; o k { / / D o s o m e t h i n g }
  8. objects c l a s s B o o k

    { p u b l i c $ t i t l e ; p u b l i c f u n c t i o n g e t A u t h o r ( ) { } } t y p e B o o k s t r u c t { T i t l e s t r i n g } f u n c ( b * B o o k ) G e t A u t h o r ( ) s t r i n g { }
  9. objects $ b o o k = n e w

    B o o k ( [ ' t i t l e ' = > ' W h e n I W o r k , T h e B o o k ' , ] ) ; $ a u t h o r = $ b o o k ­ > g e t A u t h o r ( ) ; b o o k : = & B o o k { T i t l e : " W h e n I W o r k , T h e B o o k " , } a u t h o r : = b o o k . G e t A u t h o r ( )
  10. extend objects c l a s s H a r

    d c o v e r e x t e n d s B o o k { p u b l i c f u n c t i o n i s H a r d c o v e r ( ) { r e t u r n t r u e ; } } t y p e H a r d c o v e r s t r u c t { * B o o k } f u n c ( b * H a r d c o v e r ) G e t T i t l e ( ) s t r i n g { r e t u r n f m t . S p r i n t f ( " % s [ H a r d c o v e r ] " , b . T i t l e ) / / O r r e t u r n f m t . S p r i n t f ( " % s [ H a r d c o v e r ] " , b . B o o k . T i t l e ) }
  11. our struct t y p e A u t h

    o r s t r u c t { N a m e s t r u c t { F i r s t s t r i n g L a s t s t r i n g } B o o k s [ ] B o o k }
  12. method function / / F u n c t i

    o n f u n c A d d B o o k ( a * A u t h o r , b B o o k ) { a . B o o k s = a p p e n d ( a . B o o k s , b ) } A d d B o o k ( a u t h o r , b o o k ) / / M e t h o d f u n c ( a * A u t h o r ) A d d B o o k ( b B o o k ) { a . B o o k s = a p p e n d ( a . B o o k s , b ) } a u t h o r . A d d B o o k ( b o o k ) vs
  13. more on methods / / C a n m o

    d i f y A u t h o r f u n c ( a * A u t h o r ) A d d B o o k ( b B o o k ) { a . B o o k s = a p p e n d ( a . B o o k s , b ) } / / C a n n o t m o d i f y A u t h o r f u n c ( a A u t h o r ) F u l l N a m e ( ) s t r i n g { r e t u r n a . N a m e . F i r s t + " " + a . N a m e . L a s t } But they can both be called in the same way from the struct
  14. interfaces i n t e r f a c e

    B o u n d { p u b l i c f u n c t i o n p a g e C o u n t ( ) ; } t y p e B o u n d i n t e r f a c e { P a g e C o u n t ( ) i n t }
  15. interfaces c l a s s B o o k

    i m p l e m e n t s B o u n d { p u b l i c f u n c t i o n p a g e C o u n t ( ) { r e t u r n c o u n t ( $ t h i s ­ > p a g e s ) ; } } t y p e B o o k s t r u c t { p a g e s [ ] P a g e } f u n c ( b * B o o k ) P a g e C o u n t ( ) i n t { r e t u r n l e n ( b . p a g e s ) }
  16. type casting conversion $ a u t h o r

    _ i d = ( i n t ) $ i d ; In Go, types are strict, but can still be converted i : = 4 2 f : = f l o a t 6 4 ( i ) u : = u i n t ( f )
  17. type assertion Go also has the concept of checking an

    internal type f u n c S e t A u t h o r ( o b j i n t e r f a c e { } ) { i f a u t h o r , o k : = o b j . ( s t r i n g ) ; o k { / / o b j c o n t a i n s s t r i n g / / a u t h o r i s n o w a s t r i n g } }
  18. type assertion In Go you can also check an object

    against an interface t y p e B o u n d i n t e r f a c e { P a g e C o u n t ( ) i n t } i f b o o k , o k : = b o u n d . ( B o o k ) ; o k { f m t . P r i n t l n ( b o o k . T i t l e ) }
  19. where to go from FROM HERE take the tour write

    an app concurrency experiment have fun