Hi, I've posted this on reddit and got send here.
I'm quite confused as there seems to be multiple redundant ways to solve my problem (read a file, parse the content, serve it via http).
Most people on stackoverflow would use bufio, but I just can't get the differences between this package and the Buffer type of bytes and just reading a file with the os methods.
Also I don't know when and why I should choose those ways to do it, when I have the simple, but non-versatile, ioutils.ReadFile.
This is how I solved it currently:
func loadPage(path string, f os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if !!f.IsDir() {
return nil
}
matched, err := filepath.Match("[0-9]-*.md", f.Name())
if err != nil {
panic(err)
}
if matched {
titleStart := strings.Index(f.Name(), "-") + 1
titleEnd := strings.LastIndex(f.Name(), ".md")
title := strings.ToLower(f.Name()[titleStart:titleEnd])
content, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
pages[title] = bytes.NewBuffer(blackfriday.MarkdownCommon(content))
}
return nil
}
func loadPages() {
pages = make(map[string]*bytes.Buffer)
err := filepath.Walk(PAGEDIR, loadPage)
if err != nil {
panic(err)
}
}
Besides replacing the panic calls, what could I do to improve this? I think there are too many confusing redundant possibilities...