操作系统可移植代码,用于引用模板文件

Below is the code, that runs in windows:

wd, err := os.Getwd()
if err != nil {
    log.Fatal(err)
}

t, err := template.ParseFiles(wd + "\\src\\html\\index.html")

which fails in Linux due to backslash(\)

How to make this code portable across OS?

In general using filepath.Join is a way to go:

path := filepath.Join("separate", "me")

But filepath.FromSlash is much more readable in my opinion:

path := filepath.FromSlash("separate/me")