Share
Embed
Transcript
49 slides
-
• Go microservices • gorilla/mux • sqlx, gocql • Node.js
• Feathers • Objection -
• Vue.js • Shape Detection API • Elixir + Erlang
NIF (C) account.alberta.ca -
– Brendan Eich “It was also an incredible rush job…
I knew there would be mistakes, and there would be gaps, so I made it very malleable as a language.” “a language that was approachable, that you could put directly in the web page” -
– Ryan Dahl “JavaScript plus asynchronous IO plus some HTTP
server stuff would be a cool thing. And I was so excited about that idea that I just worked on it non- stop for the next four years.” -
– Ryan Dahl, creator of Node.js “if you’re building a
server, I can’t imagine using anything other than Go” -
– TJ Holowaychuk, creator of Express.js “If you’re doing distributed
work then you’ll find Go’s expressive concurrency primitives very helpful.” -
Go origins • Unix • grep • UTF-8 encoding Ken
Thompson Robert Griesemer • Strongtalk VM • Java HotSpot VM • Codegen for V8 Rob Pike • First window system for Unix • Newsqueak, Limbo (CSP) -
– Rob Pike “We wanted a language with the safety
and performance of statically compiled languages such as C++ and Java, but the lightness and fun of dynamically typed interpreted languages such as Python.” -
• GCC toolchain Ian Lance Taylor Early gophers Brad Fitzpatrick
Russ Cox • Memcached • OpenID • Bell Labs • Tech lead -
Async/Await const axios = require("axios"); async function fetchHumans(url) {
const response = await axios.get(url); return response.data; } -
Calling an async function const express = require("express"); const app
= express(); const url = “https://www.google.com/humans.txt"; const port = 3000; app.get("/", async (req, res) => { const data = await fetchHumans(url); res.send(data); }); app.listen(port, () => { console.info(`Listening on ${port}`); }); -
Promises all the way down Whoops! app.get("/", async (req, res)
=> { const data = await fetchHumans(url); res.send(data); }); The result: Promise { <pending> } -
Multiple promises const urls = [ "https://www.google.com/humans.txt", "https://www.netflix.com/humans.txt", "https://medium.com/humans.txt", ];
app.get("/", async (req, res) => { const promises = urls.map((url) => fetchHumans(url)); const data = await Promise.all(promises); res.setHeader("content-type", "text/plain"); res.send(data.join("\n\n")); }); -
We need error handling const url = "https://www.moogle.com/humans.txt"; The result:
UnhandledPromiseRejectionWarning Error: connect ECONNREFUSED -
Try/Catch app.get("/", async (req, res) => { try { const
data = await fetchHumans(url); res.send(data); } catch (err) { console.error(err.message); res.status(502).send("Bad gateway"); } }); -
First error app.get("/", async (req, res) => { try {
const promises = urls.map((url) => fetchHumans(url)); const data = await Promise.all(promises); res.setHeader("content-type", "text/plain"); res.send(data.join("\n\n")); } catch (err) { console.error(err.message); res.status(502).send("Bad gateway"); } }); -
It looks synchronous package main import ( "io" "log"
"net/http" ) func fetch(url string) (string, error) { r, err := http.Get(url) if err != nil { return "", err } defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) if err != nil { return "", err } return string(b), nil } -
Response writer const url = "https://www.google.com/humans.txt" func handler(w http.ResponseWriter,
req *http.Request) { data, err := fetch(url) if err != nil { log.Print(err) w.WriteHeader(http.StatusBadGateway) io.WriteString(w, "Bad gateway") return } io.WriteString(w, data) } -
Listen and serve func main() { http.HandleFunc("/", handler) const
addr = ":3000" log.Printf("Listening on %v", addr) log.Fatal(http.ListenAndServe(addr, nil)) } -
Fetch multiple humans.txt package main import ( "io" "io/ioutil"
"log" "net/http" "strings" "sync" ) var urls = []string{ "https://www.google.com/humans.txt", "https://www.netflix.com/humans.txt", "https://medium.com/humans.txt", } -
A synchronous API func handler(w http.ResponseWriter, req *http.Request) { data,
err := fetchHumans(urls) if err != nil { log.Print(err) w.WriteHeader(http.StatusBadGateway) io.WriteString(w, "Bad gateway") return } w.Header().Set("Content-Type", "text/plain; charset=utf-8") io.WriteString(w, strings.Join(data, "\n\n")) } -
Concurrency primitives func fetchHumans(urls []string) ([]string, error) { var m
sync.Mutex var wg sync.WaitGroup errChan := make(chan error, 1) resp := make([]string, len(urls)) -
for index, url := range urls { wg.Add(1) go func(index
int, url string) { defer wg.Done() s, err := fetch(url) if err != nil { select { case errChan <- err: default: } return } m.Lock() resp[index] = s m.Unlock() }(index, url) } wg.Wait() select { case err := <-errChan: return nil, err default: } return resp, nil } -
JavaScript Go Promises Channels Single-threaded* event loop Multiplexed goroutines async/await
Appears synchronous Async and Concurrency -
Destructuring const data = { latitude: 53.5458874, longitude: -113.5034304, timezone:
"America/Edmonton", currently: { summary: "Light Snow", temperature: 20.33, }, }; const { currently: {summary, temperature}, } = data; console.info(`${summary} and ${temperature}ºF`); -
– Brendan Eich on early feature requests “I’d like to
compare a number to a string that contains that numeral. And I don’t want to have to change my code to convert the string to a number, or the number to a string. I just want it to work. Can you please make the equals operator just say, Oh this looks like a two, and this looks like a string number two. They’re equal enough.” -
Incomparable if "2" == 2 { // } The result:
invalid operation: "2" == 2 (mismatched types string and int) -
– Go brand book “Go is an open source programming
language that enables the production of simple, efficient, and reliable software at scale.” -
Mutable iteration variables for index, url := range urls {
go func(index int, url string) { // .. }(index, url) } -
JavaScript Go Dynamically typed Statically typed Lenient Strict compiler Prototypes,
Classes, Inheritance Types, methods, interfaces First-class functions First-class functions Language -
Node.js Go prettier gofmt eslint go lint, go vet Babel,
TypeScript go build Jest, Chai, Mocha, Jasmine go test Tooling -
Node.js Go HTTP/2 HTTP/2 TLS 1.3 with OpenSSL 1.1.1 TLS
1.3 in Go 1.12 Modern Web -
Node.js Go Full stack Server-only* Ecosystem Batteries included Small projects*
Large projects, large teams I/O bound CPU bound and I/O bound Malleable language Reliability, security Use when