Standard library / io/fs

fs.Glob()

Returns names matching path.Match syntax; nil means no matches. Filesystem traversal errors are ignored; malformed pattern is reported. Expected stdout: '[a.txt] <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func Glob(fsys FS, pattern string) (matches []string, err error)

Parameters & Attributes

NameTypeDescription
fsysFSFilesystem implementing fs.FS.
patternstringpath.Match pattern.

Return Value

(matches []string, err error)

Returns names matching path.Match syntax; nil means no matches.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io/fs"  "testing/fstest"  "time")
func tree() fstest.MapFS {  return fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("ok"), Mode: 0644, ModTime: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)}, "sub/b.txt": &fstest.MapFile{Data: []byte("go"), Mode: 0644}}}func main() { m, e := fs.Glob(tree(), "*.txt"); fmt.Println(m, e) }
Go Reference