Standard library / path/filepath

path/filepath.Glob()

Returns paths matching a pattern in lexical order; no matches yields nil, and malformed patterns return an error. Directory I/O errors are ignored. Example uses a disposable temporary workspace. Expected stdout: '[a.txt]\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
patternstringShell-style matching pattern.

Return Value

(matches []string, err error)

Returns paths matching a pattern in lexical order; no matches yields nil, and malformed patterns return an error.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()(); seed(); m, e := filepath.Glob("*.txt"); check(e); fmt.Println(m) }
Go Reference