Share
Embed
Escrevendo Go no front-end com GopherJS - Go Metup
O GopherJS é um compilador de Go para JavaScript, mas quais são os beneficios e maleficios que nós desenvolvedores temos com uso do GopherJS?
Felipe Oliveira
August 17, 2016
Transcript
34 slides
-
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 1/34 Escrevendo Go no
front-end com GopherJS 17 August 2016 Felipe Paes de Oliveira -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 2/34 Sobre mim Atualmente
trabalho como desenvolvedor/instrutor na Caelum e Alura Lider de desenvolvimento do Mamute github.com/caelum/mamute (https://github.com/caelum/mamute) Autor da lib Gopher Utils github.com/felipeweb/gopher-utils (https://github.com/felipeweb/gopher-utils) Autor da lib Osin Mysql github.com/felipeweb/osin-mysql (https://github.com/felipeweb/osin-mysql) Contribuidor do framework Go Macaron https://github.com/go-macaron/macaron (.link) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 3/34 Quais plataformas suportam
Go? Linux (Go nativo), Google OSX (Go nativo), Google Windows (Go nativo), Google Raspberry Pi (Go nativo), Google Android (Go mobile), Google IOS (Go mobile), Google Browser (Gopher JS), Comunidade, com participação de commiters da linguagem Go -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 4/34 O que GopherJS
suporta? 100% da Go standard library Grande parte da implementação segue a especi cação do Go Defer Panic Recover Goroutines -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 5/34 Por que usar
GopherJS? Oportunidade de escrever código Go para rodar em todos os navegadores Familiaridade com ferramentas (gofmt, godoc, ...) Familiaridade com tipos (int, bytes, ...) Checagem de tipos Familiaridade com concorrência, blocking receiving ao invés de callbacks Familiaridade com erros de compilação (refatoração) Uso de bibliotecas de terceiros (Go e JavaScript) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 7/34 Adicionando GopherJS ao
$GOPATH g o g e t - u g i t h u b . c o m / g o p h e r j s / g o p h e r j s -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 8/34 Hello GopherJS Hello
com JavaScript c o n s o l e . l o g ( " H e l l o J S " ) ; Hello 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 l n ( " H e l l o J S w i t h G o " ) } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 9/34 Hello GopherJS HTML
< ! D O C T Y P E h t m l > < h t m l l a n g = " e n " > < h e a d > < m e t a c h a r s e t = " U T F - 8 " > < t i t l e > H e l l o < / t i t l e > < / h e a d > < b o d y > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " h e l l o j s . j s " > < / s c r i p t > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " h e l l o j s w i t h g o . j s " > < / s c r i p t > < / b o d y > < / h t m l > Compilando o arquivo Go para JavaScript g o p h e r j s b u i l d - o h e l l o j s w i t h g o . j s /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro-gopherJS/hello.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 10/34 Manipulando o DOM
com GopherJS DOM Go p a c k a g e m a i n i m p o r t " g i t h u b . c o m / g o p h e r j s / g o p h e r j s / j s " f u n c m a i n ( ) { j s . G l o b a l . G e t ( " w i n d o w " ) . S e t ( " o n l o a d " , f u n c ( ) { g o f u n c ( ) { b o d y : = j s . G l o b a l . G e t ( " d o c u m e n t " ) . G e t ( " b o d y " ) b o d y . G e t ( " s t y l e " ) . S e t ( " b a c k g r o u n d - c o l o r " , " r e d " ) i m g : = j s . G l o b a l . G e t ( " d o c u m e n t " ) . C a l l ( " c r e a t e E l e m e n t " , " i m g " ) i m g . S e t ( " s r c " , " g o p h e r . p n g " ) b o d y . C a l l ( " a p p e n d C h i l d " , i m g ) } ( ) } ) } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 11/34 Manipulando o DOM
com GopherJS HTML < ! D O C T Y P E h t m l > < h t m l l a n g = " e n " > < h e a d > < m e t a c h a r s e t = " U T F - 8 " > < t i t l e > D O M < / t i t l e > < / h e a d > < b o d y > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " d o m . j s " > < / s c r i p t > < / b o d y > < / h t m l > Compilando o arquivo Go para JavaScript g o p h e r j s b u i l d d o m . g o - o d o m . j s /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro-gopherJS/dom.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 12/34 Manipulando o DOM
com GopherJS (DOM lib) Instalando a biblioteca de DOM g o g e t - u h o n n e f . c o / g o / j s / d o m DOM com a lib Go p a c k a g e m a i n i m p o r t " h o n n e f . c o / g o / j s / d o m " f u n c m a i n ( ) { d o m . G e t W i n d o w ( ) . A d d E v e n t L i s t e n e r ( " l o a d " , f a l s e , f u n c ( e d o m . E v e n t ) { g o f u n c ( ) { b o d y : = d o m . G e t W i n d o w ( ) . D o c u m e n t ( ) . G e t E l e m e n t B y I D ( " b o d y " ) b o d y . S e t A t t r i b u t e ( " s t y l e " , " b a c k g r o u n d - c o l o r : r e d " ) i m g : = d o m . G e t W i n d o w ( ) . D o c u m e n t ( ) . C r e a t e E l e m e n t ( " i m g " ) i m g . S e t A t t r i b u t e ( " s r c " , " g o p h e r . p n g " ) b o d y . A p p e n d C h i l d ( i m g ) } ( ) } ) } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 13/34 Manipulando o DOM
com GopherJS (DOM lib) HTML < ! D O C T Y P E h t m l > < h t m l l a n g = " e n " > < h e a d > < m e t a c h a r s e t = " U T F - 8 " > < t i t l e > D O M < / t i t l e > < / h e a d > < b o d y i d = " b o d y " > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " d o m _ w i t h _ l i b . j s " > < / s c r i p t > < / b o d y > < / h t m l > Compilando o arquivo Go para JavaScript g o p h e r j s b u i l d d o m _ w i t h _ l i b . g o - m - o d o m _ w i t h _ l i b . j s /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro- gopherJS/dom_with_lib.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 15/34 Tem uma lib
que encapsula o Jquery para GopherJS? -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 16/34 Jquery GopherJS Adicionando
Jquery ao $GOPATH g o g e t - u g i t h u b . c o m / g o p h e r j s / j q u e r y DOM Go com Jquery p a c k a g e m a i n i m p o r t " g i t h u b . c o m / g o p h e r j s / j q u e r y " f u n c m a i n ( ) { j q u e r y . N e w J Q u e r y ( ) . R e a d y ( f u n c ( ) { j q u e r y . N e w J Q u e r y ( " b o d y " ) . S e t C s s ( " b a c k g r o u n d - c o l o r " , " r e d " ) j q u e r y . N e w J Q u e r y ( " b o d y " ) . A p p e n d ( " < i m g s r c = \ " g o p h e r . p n g \ " / > " ) } ) } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 17/34 Jquery GopherJS HTML
< ! D O C T Y P E h t m l > < h t m l l a n g = " e n " > < h e a d > < m e t a c h a r s e t = " U T F - 8 " > < t i t l e > j q u e r y < / t i t l e > < / h e a d > < b o d y > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " h t t p s : / / c o d e . j q u e r y . c o m / j q u e r y - 3 . 1 . 0 . m i n . j s " > < / s c r i p t > < s c r i p t t y p e = " t e x t / j a v a s c r i p t " s r c = " d o m _ w i t h _ j q u e r y . j s " > < / s c r i p t > < / b o d y > < / h t m l > Compilando o arquivo Go para JavaScript g o p h e r j s b u i l d d o m _ w i t h _ j q u e r y . g o - m - o d o m _ w i t h _ j q u e r y . j s /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro- gopherJS/dom_with_jquery.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 18/34 Ajax GopherJS p
a c k a g e m a i n i m p o r t ( " n e t / h t t p " " i o / i o u t i l " " g i t h u b . c o m / f e l i p e w e b / g o p h e r - u t i l s " " h o n n e f . c o / g o / j s / d o m " ) f u n c m a i n ( ) { r e s , e r r : = h t t p . G e t ( " h t t p s : / / c u r s o s . a l u r a . c o m . b r / a p i / c u r s o s " ) i f e r r ! = n i l { p a n i c ( e r r ) } b y t e s , e r r : = i o u t i l . R e a d A l l ( r e s . B o d y ) i f e r r ! = n i l { p a n i c ( e r r ) } d : = d o m . G e t W i n d o w ( ) . D o c u m e n t ( ) p : = d . C r e a t e E l e m e n t ( " p " ) . ( * d o m . H T M L P a r a g r a p h E l e m e n t ) p . S e t T e x t C o n t e n t ( g o p h e r _ u t i l s . T o S t r ( b y t e s ) ) c o n t e n t : = d . G e t E l e m e n t B y I D ( " c o n t e n t " ) . ( * d o m . H T M L D i v E l e m e n t ) c o n t e n t . A p p e n d C h i l d ( p ) } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 20/34 Como o gopherJS
lida com as peculiaridades do Go? -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 21/34 Defer GopherJS p
a c k a g e m a i n i m p o r t ( " f m t " " t i m e " ) f u n c m a i n ( ) { d e f e r w o r l d ( ) f m t . P r i n t l n ( " H e l l o " ) } f u n c w o r l d ( ) { t i m e . S l e e p ( 5 * t i m e . S e c o n d ) f m t . P r i n t l n ( " W o r l d " ) } O defer é resovido em tempo de compilação do código para JavaScript /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro-gopherJS/defer.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 22/34 Panic e Recover
f u n c P a r s e ( i n p u t s t r i n g ) ( n u m b e r s [ ] i n t , e r r e r r o r ) { d e f e r f u n c ( ) { i f r : = r e c o v e r ( ) ; r ! = n i l { v a r o k b o o l e r r , o k = r . ( e r r o r ) i f ! o k { e r r = f m t . E r r o r f ( " p k g : % v " , r ) } } } ( ) f i e l d s : = s t r i n g s . F i e l d s ( i n p u t ) n u m b e r s = f i e l d s 2 n u m b e r s ( f i e l d s ) r e t u r n } -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 23/34 Panic e Recover
Panic é convertido para o tipo Error do JavaScript Recover é controlado por meio de try e catch /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro-gopherJS/recover.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 24/34 Goroutines e Chanels
/ / s t a r t t o m o v e s n a k e t i c k e r : = t i m e . N e w T i c k e r ( s p e e d ) q u i t : = m a k e ( c h a n s t r u c t { } ) g o f u n c ( ) { f o r { s e l e c t { c a s e < - t i c k e r . C : s n a k e M o v e ( q u i t , d ) c a s e < - q u i t : t i c k e r . S t o p ( ) r e t u r n } } } ( ) github.com/felipeweb/talks/blob/master/intro-gopherJS/snake.go (https://github.com/felipeweb/talks/blob/master/intro- gopherJS/snake.go) O GopherJS ca manipulando a pilha de instruções a serem executadas /Users/felipeweb/projetos/go/src/github.com/felipeweb/talks/intro-gopherJS/snake.html (#ZgotmplZ) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 25/34 Além da lib
Jquery e DOM existe outras :) AngularJS: github.com/neelance/go-angularjs (https://github.com/neelance/go-angularjs) Canvas: github.com/emersion/go-js-canvas (https://github.com/emersion/go-js-canvas) Chrome API github.com/fabioberger/chrome (https://github.com/fabioberger/chrome) D3: github.com/iansmith/d3 (https://github.com/iansmith/d3) DOM: honnef.co/go/js/dom (http://honnef.co/go/js/dom) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 26/34 Além da lib
Jquery e DOM existe outras :) EventSource github.com/gopherjs/eventsource (https://github.com/gopherjs/eventsource) JQuery: github.com/gopherjs/jquery (https://github.com/gopherjs/jquery) JS Builtins: github.com/gopherjs/jsbuiltin (https://github.com/gopherjs/jsbuiltin) JS console: honnef.co/go/js/console (http://honnef.co/go/js/console) Polymer: code.palmstonegames.com/polymer (https://code.palmstonegames.com/polymer) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 27/34 Além da lib
Jquery e DOM existe outras :) VueJS: github.com/oskca/gopherjs-vue (https://github.com/oskca/gopherjs-vue) localStorage: github.com/go-humble/locstor (https://github.com/go-humble/locstor) PouchDB: github.com/ imzy/go-pouchdb (https://github.com/ imzy/go-pouchdb) QUnit: github.com/rusco/qunit (https://github.com/rusco/qunit) SQL.js: github.com/ imzy/go-sql.js (https://github.com/ imzy/go-sql.js) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 28/34 Além da lib
Jquery e DOM existe outras :) WebGL: github.com/gopherjs/webgl (https://github.com/gopherjs/webgl) WebSocket: github.com/gopherjs/websocket (https://github.com/gopherjs/websocket) XHR: honnef.co/go/js/xhr (http://honnef.co/go/js/xhr) Mithril: github.com/danverbraganza/go-mithril (https://github.com/danverbraganza/go-mithril) Ace: maunium.net/go/gopher-ace (https://maunium.net/go/gopher-ace) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 31/34 Qual é o
cenério ideal para se usar GopherJS? Angular JS React Vecty (framework react like criado pela comunidade GopherJS) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 32/34 Sobre o Vecty
Código github.com/gopherjs/vecty (https://github.com/gopherjs/vecty) Exemple github.com/gopherjs/vecty/tree/master/examples/todomvc (https://github.com/gopherjs/vecty/tree/master/examples/todomvc) Server localhost:8080/github.com/gopherjs/vecty/examples/todomvc/ (http://localhost:8080/github.com/gopherjs/vecty/examples/todomvc/) -
9/12/2016 Escrevendo Go no front-end http://localhost:3999/intro-gopherJS/intro_gopherJS.slide#1 33/34 Thank you Felipe
Paes de Oliveira [email protected] (mailto:[email protected]) https://www.felipeweb.net.br (https://www.felipeweb.net.br) https://github.com/felipeweb (https://github.com/felipeweb) @_felipeweb (http://twitter.com/_felipeweb)