Skip to content

Instantly share code, notes, and snippets.

@will3942
will3942 / index.html
Last active December 30, 2015 06:59
Go template demo
<html>
<body>
<h1>Will's Blog!</h1>
<p>{{.}}</p>
</body>
</html>
@will3942
will3942 / hello_world.go
Created December 4, 2013 18:46
Go: Hello World!
package main
import "fmt"
func main() {
fmt.Printf("Hello World!\n")
}
@will3942
will3942 / web_server.go
Last active December 30, 2015 06:59
Go: Web Server
package main
import (
"fmt"
"net/http"
)
func handlerequest(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi you accessed post %s", r.URL.Path[1:])
}
@will3942
will3942 / template.go
Created December 4, 2013 18:50
Go: template
package main
import (
"html/template"
"net/http"
)
func handlerequest(w http.ResponseWriter, r *http.Request) {
title := "Hello World!"
t := template.New("index.html")
@will3942
will3942 / index_final.html
Created December 4, 2013 19:47
Go: Final Homepage Template
<html>
<body>
<h1>Will's Blog!</h1>
{{range .}}
<a href="/{{.File}}"><h2>{{.Title}} ({{.Date}})</h2></a>
<p>{{.Summary}}</p>
{{end}}
</body>
</html>
@will3942
will3942 / test.txt
Last active December 30, 2015 07:09
Go: First markdown post
First post!
4th December 2013
This is the summary.
This is the main post!
# Markdown!
*it's* **lovely**!
@will3942
will3942 / blog_index_only.go
Created December 4, 2013 20:52
Go: displaying blog posts on the home page.
package main
import (
"html/template"
"net/http"
"path/filepath"
"strings"
"io/ioutil"
"github.com/russross/blackfriday"
)
@will3942
will3942 / imports.go
Created December 4, 2013 21:00
Go: new imports
import (
"html/template"
"net/http"
"path/filepath"
"strings"
"io/ioutil"
"github.com/russross/blackfriday"
)
@will3942
will3942 / posts_object.go
Created December 4, 2013 21:01
Go: posts object
type Post struct {
Title string
Date string
Summary string
Body string
File string
}
@will3942
will3942 / get_posts.go
Created December 4, 2013 21:02
Go: getPosts() function
func getPosts() []Post{
a := []Post{}
files, _ := filepath.Glob("posts/*")
for _, f := range files {
file := strings.Replace(f, "posts/", "", -1)
file = strings.Replace(file, ".md", "", -1)
fileread, _ := ioutil.ReadFile(f)
lines := strings.Split(string(fileread), "\n")
title := string(lines[0])
date := string(lines[1])