-
-
Save lswest/feed0fb8685b0d9bed03e864a78f7f1a to your computer and use it in GitHub Desktop.
Example Go Project for FCM 121
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ define "header" }} | |
<header> | |
<nav> | |
<a href="#">Example</a> | |
<a href="#">Example</a> | |
</nav> | |
</header> | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"strings" | |
"html/template" | |
) | |
type Page struct { | |
Title string | |
Content string | |
} | |
func viewHandler(w http.ResponseWriter, r *http.Request) { | |
hello := Page{"Hello World", "This is a hello world page"} | |
if(len(r.URL.Path) > len("/")) { | |
person := r.URL.Path[len("/"):] | |
hello = Page{"Hello Person", "Hello, "+person} | |
} | |
funcMap := template.FuncMap{ | |
"ToLower": strings.ToLower, | |
} | |
tpl := template.Must(template.New("hello.html").Funcs(funcMap).ParseGlob("tmpl/*")) | |
tpl.ExecuteTemplate(w, "hello.html", hello) | |
} | |
func main() { | |
http.HandleFunc("/", viewHandler) | |
http.ListenAndServe(":8081", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>{{ .Title | html}}</title> | |
</head> | |
<body> | |
{{ template "header" }} | |
<h1>{{ .Title | html }}</h1> | |
<p>all lowercase: {{ .Title | ToLower }}</p> | |
<main> | |
<p>{{ .Content | html }}</p> | |
</main> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment