Search code examples
gofilepath

How to add a trailing slash when joining paths


Im making a webapp and I'm trying to write views/layouts/*.html using two vars but filepath.Join only gives views/layouts instead of views/layouts/

var (
    LayoutDir string = filepath.Join("views","layouts")
    TemplateExt string = "*.html"
)

then calling

f, err:= filepath.Glob(LayoutDir + TemplateExt)

And so f contains views/layouts*.html instead of views/layouts/*.html How should I solve this and is it a good practice to use filepath.Join in this case?


Solution

  • f, err:= filepath.Glob(filepath.Join(LayoutDir, TemplateExt)) should do the trick

    or

    LayoutDir string = filepath.Join("views", "layouts") + string(filepath.Separator)

    or

    LayoutDir string = filepath.Join("views", "layouts") + string(os.PathSeparator)