安装Markdown解析库:https://github.com/russross/blackfriday
第一版:go get github.com/russross/blackfriday
第二版:go get -u gopkg.in/russross/blackfriday.v2
package main
import (
"net/http"
"fmt"
"path/filepath"
"strings"
"io/ioutil"
//"github.com/russross/blackfriday"
"gopkg.in/russross/blackfriday.v2"
)
func handleRequest(w http.ResponseWriter, r *http.Request) {
fp, _ := filepath.Abs("")
fn := fp + r.URL.Path;
fmt.Println(fn);
if(!strings.HasSuffix(fn, ".md")){
http.ServeFile(w, r, fn)
}else{
fileread, _ := ioutil.ReadFile(fn)
//fmt.Fprintln(w, string(blackfriday.MarkdownCommon([]byte(fileread))))
fmt.Fprintln(w, string(blackfriday.Run([]byte(fileread))))
}
}
func main() {
http.HandleFunc("/", handleRequest)
http.ListenAndServe(":8000", nil)
}
终端:go run md.go
浏览器:localhost:8000