The document provides an overview of building web applications in Go. It discusses how Go allows creating self-contained binaries without wrappers like Apache or Nginx. It demonstrates a simple Hello World web app in Go and shows how to use templates to render HTML. It also covers routing, middleware, sessions, deployment to Google App Engine, and examples of real web apps built and deployed with Go.
6. Web Apps in Go
• No need for a wrapper like Apache or Nginx
• Create a self-contained, statically compiled
binary
• Cross Compile from your laptop to your
server architecture
7. Simple Web App
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, worldn")
})
fmt.Print("Listening on :8080n")
http.ListenAndServe(":8080", nil)
}
8. Templates
Easily render well formed HTML using data
from your application
Nest templates inside each other (partials) for
easy re-use
9. Simple Template Example
func handleRoot(w http.ResponseWriter, req *http.Request) {
var templateStr =
"<html><body><h1>{{.}}</h1></body></html>n"
var templ =
template.Must(template.New("qr").Parse(templateStr))
templ.Execute(w, "Hello!")
}
func main() {
http.HandleFunc("/", handleRoot)
fmt.Print("Listening on :8080n")
http.ListenAndServe(":8080", nil)
}
14. It’s the Methods, man!
func uploadHandler(…) {
if r.Method != "POST" {
err := doPostThings()
handleErrors(err)
return
}
}
15. Routing
Use a routing library like httprouter or mux to
match requests to handlers
Routes defined using paths and HTTP verbs
r := httprouter.New()
r.POST(”/login", LoginHandler)
r.GET("/", HomeHandler)
19. Render
• Cleans Up boilerplate template rendering code
• Handles Errors
• Takes care of headers for HTML, JSON, XML
etc
https://github.com/unrolled/render
20. Context
• Stores values shared during a request
lifetime
• Useful for easily passing the full context of a
request to multiple handlers
http://www.gorillatoolkit.org/pkg/context
21. Sessions
Keep data between requests to maintain state
• User information
• Shopping Cart
• ETC
http://www.gorillatoolkit.org/pkg/sessions
30. Google App Engine
• GAE supports Go runtime
• Automatically scales up instances
• Provides highly available datastore
• Supports Go 1.4 at this time
• Soon: Migrate GAE to Managed VMs
https://cloud.google.com/appengine/
34. Other Resources
Building Web Apps with Go – by codegangtsa
http://bit.ly/WebAppsWithGo
Google Cloud Platform - Go
http://bit.ly/GoGoogleCloud
35. Deployed To GAE
SMS Voting Application for CSS Dev Conf
• Takes votes via SMS (Twilio) and stores
them in GAE Datastore
• Outputs them as CSV
36. Deployed to GAE
OthrNumbr
• Burner Numbers for
texting with strangers
• Written in Go, Deployed
on App Engine
• Users Datastore,
memcache, Twilio, Stripe
http://othrnumbr.com/
39. Concurrency in Go
• In Go, concurrency is accomplished by
passing shared values around on channels
• Not by sharing memory between threads
40. Repeat After Me
“Do not communicate by sharing
memory; instead, share memory by
communicating.”
41. Concurrent Routines
Created by putting “go” in front of function calls
func Announce(message string, delay time.Duration) {
go func() {
time.Sleep(delay)
fmt.Println(message)
}() // Note the parentheses - must call the function.
}
https://golang.org/doc/effective_go.html#concurrency
42. Channels
Go has a built in primitive types for
communicating between goroutines
c := make(chan int) // Allocate a channel.
// Start the sort in a goroutine; when it completes, signal on the channel.
go func() {
list.Sort()
c <- 1 // Send a signal; value does not matter.
}()
doSomethingForAWhile()
<-c // Wait for sort to finish; discard sent value.
43. Channels
• Can be buffered or unbuffered
• Can be declared to carry any other type,
even channels!
44. Concurrency in Go
The essential guides:
http://bit.ly/ConcurrentGo
http://bit.ly/AdvancedGoConcurrency
Hinweis der Redaktion
Hello everyone, thank you for coming. I’m Andy Watson and I’m here to talk to you about ways to use cryptography correctly in your applications
I’m currently a senior engineer at Ionic Security which is a data protection security company based out of Atlanta, GA
I’ve been a software developer professionally since 1996 when I got my first job developing large scale, distributed systems for processing streams of data collected out of particle accelerators with some Physics professors at FSU. This was “cloud” computing before it had a name.
Since then I’ve built mobile, desktop and web applications for companies like The Walt Disney World Resort, Maersk Sealand, Cox Communications, CoffeeCup Software and many many others.