Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bonfy/go-mega-code
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 01-Hello-World
Choose a base ref
...
head repository: bonfy/go-mega-code
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 02-Template
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Sep 6, 2018

  1. Copy the full SHA
    1cf55f6 View commit details

Commits on Sep 7, 2018

  1. Copy the full SHA
    cca162a View commit details
  2. 02-Template if-else

    bonfy committed Sep 7, 2018
    Copy the full SHA
    9df5fb2 View commit details
  3. 02-Template range

    bonfy committed Sep 7, 2018
    Copy the full SHA
    50e5028 View commit details
Showing with 49 additions and 2 deletions.
  1. +33 −2 main.go
  2. +16 −0 templates/index.html
35 changes: 33 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
package main

import "net/http"
import (
"html/template"
"net/http"
)

// User struct
type User struct {
Username string
}

// Post struct
type Post struct {
User User
Body string
}

// IndexViewModel struct
type IndexViewModel struct {
Title string
User User
Posts []Post
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
u1 := User{Username: "bonfy"}
u2 := User{Username: "rene"}

posts := []Post{
Post{User: u1, Body: "Beautiful day in Portland!"},
Post{User: u2, Body: "The Avengers movie was so cool!"},
}

v := IndexViewModel{Title: "Homepage", User: u1, Posts: posts}
tpl, _ := template.ParseFiles("templates/index.html")
tpl.Execute(w, &v)
})
http.ListenAndServe(":8888", nil)
}
16 changes: 16 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
{{if .Title}}
<title>{{.Title}} - blog</title>
{{else}}
<title>Welcome to blog!</title>
{{end}}
</head>
<body>
<h1>Hello, {{.User.Username}}!</h1>
{{range .Posts}}
<div><p>{{ .User.Username }} says: <b>{{ .Body }}</b></p></div>
{{end}}

</body>
</html>