4. About Me
Lawyer (1-2 yrs)
Learning Journey
May – Jul 2014: HTML, CSS, JS
5. About Me
Lawyer (1-2 yrs)
Learning Journey
May – Jul 2014: HTML, CSS, JS
(codeacademy, books,
websites)
6. About Me
Lawyer (1-2 yrs)
Learning Journey
(codeacademy, books,
websites)
May – Jul 2014: HTML, CSS, JS
mid-Jul 2014: Go (1st backend)
7. About Me
Lawyer (1-2 yrs)
Learning Journey
(codeacademy, books,
websites)
(everywhere)
May – Jul 2014: HTML, CSS, JS
mid-Jul 2014: Go (1st backend)
8. About Me
Lawyer (1-2 yrs)
Learning Journey
(codeacademy, books,
websites)
(everywhere)
May – Jul 2014: HTML, CSS, JS
mid-Jul 2014: Go (1st backend)
mid-Aug 2014: deployed 1st app – CRUD app
9. About Me
Lawyer (1-2 yrs)
Learning Journey
(codeacademy, books,
websites)
(everywhere)
May – Jul 2014: HTML, CSS, JS
mid-Jul 2014: Go (1st backend)
mid-Aug 2014: deployed 1st app – CRUD app
end-Aug 2014: deployed 2nd app – API client
10. Go Resources
golang.org
Books and websites
Sample applications
Stackoverflow, Go-nuts mailing list
11. Go Resources
golang.org (Go Tour, Docs, Go blog)
Books and websites
Sample applications
Stackoverflow, Go-nuts mailing list
12. Go Resources
golang.org (Go Tour, Docs, Go blog)
Books and websites (www.golang-book.com, www.gobyexample.com)
Sample applications
Stackoverflow, Go-nuts mailing list
13. Go Resources
golang.org (Go Tour, Docs, Go blog)
Books and websites (www.golang-book.com, www.gobyexample.com)
Sample applications (Go Wiki, from books)
Stackoverflow, Go-nuts mailing list
19. Basic Structure
package main
Import (
“fmt”
“net/http”
)
All executable commands use package main
Print output to client
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, “hello world!”)
}
func main() {
Handler function when root is
requested
http.HandleFunc(“/”, handler)
http.ListenAndServe(“:8080”, nil)
}
Local server connection
Go’s stdlib
Entry point
22. Data Structs
Type Slice of Pointers to Post struct
type User struct {
Userid int64
Username string
Password string
Posts []*Post
}
type Post struct {
Tweetid string
Username string
Status string
}
23. Database functions
func ReadStatus() (res [][]string) { Return slice of slice of strings
err := db.Ping()
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE
username = ? ORDER BY id DESC", currentuser)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var tweet, id, username string
for rows.Next() {
err := rows.Scan(&id, &tweet, &username)
if err != nil {
return res
}
var a []string
a = append(a, id, tweet, username)
res = append(res, a)
}
return
}
24. Database functions
func ReadStatus() (res [][]string) { Return slice of slice of strings
err := db.Ping()
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE
username = ? ORDER BY id DESC", currentuser)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var tweet, id, username string
for rows.Next() {
err := rows.Scan(&id, &tweet, &username)
if err != nil {
return res
}
var a []string
a = append(a, id, tweet, username)
res = append(res, a)
}
return
}
Verify db connection
Raw queries
Append strings to slice
Append slice of strings to output
25. Handler functions
func homeHandler(w http.ResponseWriter, r *http.Request) {
Create slice with Post instances
Append slice of Post
instances to Posts slice
currentuser = getUserName(r)
if currentuser != "" {
var as []Post
a := User{}
m := ReadStatus()
for i := 0; i < len(m); i++ {
as = append(as, Post{Tweetid: m[i][0], Username:
currentuser, Status: m[i][1]})
}
a = User{Username: currentuser}
for i := 0; i < len(m); i++ {
a.Posts = append(a.Posts, &as[i])
}
renderTemplate(w, "home", "homepage", a)
} else {
http.Redirect(w, r, "/", 302)
}
}
Wrapper func around
html/template library
26. Func main
Open connection pool
Handlers for URL paths
func main() {
db, err = sql.Open("mysql",
os.Getenv("DB_USERNAME")+":"+os.Getenv("DB_PASSWORD")+"@tcp("+os.Getenv("DB
_CLEARDB")+":3306)/"+os.Getenv("DB_NAME"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
router.HandleFunc("/", logHandler)
router.HandleFunc("/login", loginHandler)
router.HandleFunc("/home", homeHandler)
router.HandleFunc("/home/tweets", usertweetHandler).Methods("POST")
router.HandleFunc("/logout", logoutHandler).Methods("POST")
router.HandleFunc("/home/delete", deleteHandler).Methods("POST")
router.HandleFunc("/home/edit", editHandler).Methods("POST")
router.HandleFunc("/home/save", saveHandler).Methods("POST”)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
http.Handle("/", router)
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}
Port for server
connection
27. 2nd app: API Client
Get a series of photos from Flickr
for a random city and display
current weather data for the same
city