SoFunction
Updated on 2025-03-01

Example of regular expression search function of go language file

package main
import (
    "fmt"
    "os"
    "path/filepath"
    "regexp"
)
func main() {
// Command line parameters
    args :=
// Check parameters
    if len(args) == 1 {
        ("ff is a file find tool. use like bottom")
        ("ff [dir] [regexp]")
        return
    }
    if len(args) < 3 {
        ("args < 3")
        return
    }
    fileName := args[1]
    pattern := args[2]
    file, err := (fileName)
    if err != nil {
        (err)
        return
    }
    fi, err := ()
    if err != nil {
        (err)
        return
    }
    if !() {
        (fileName, " is not a dir")
    }
    reg, err := (pattern)
    if err != nil {
        (err)
        return
    }
//Travel the directory
    (fileName,
        func(path string, f , err error) error {
            if err != nil {
                (err)
                return err
            }
            if () {
                return nil
            }
// Match directory
            matched := (())
            if matched {
                (path)
            }
            return nil
        })
}