func TestFindFileType(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "foo_file"))
	os.Mkdir(filepath.Join(dir, "foo_dir"), 0777)
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   "foo",
		"directory": dir,
		"filetype":  "f",
	}, outputBuf)
	output := outputBuf.String()
	if !strings.Contains(output, "foo_file") {
		t.Errorf("Must contain 'foo_file' in %s", output)
	}
	if strings.Contains(output, "foo_dir") {
		t.Errorf("Shouldn't contain 'foo_dir' in %s", output)
	}
	outputBuf = bytes.NewBufferString("")  
	Find(map[string]string{
		"pattern":   "foo",
		"directory": dir,
		"filetype":  "d",
	}, outputBuf)
	output = outputBuf.String()
	if !strings.Contains(output, "foo_dir") {
		t.Errorf("Must contain 'foo_dir' in %s", output)
	}
	if strings.Contains(output, "foo_file") {
		t.Errorf("Shouldn't contain 'foo_file' in %s", output)
	}
	outputBuf = bytes.NewBufferString("")  
	Find(map[string]string{
		"pattern":   "foo",
		"directory": dir,
		"filetype":  "f,d",
	}, outputBuf)
	output = outputBuf.String()
	if !strings.Contains(output, "foo_dir") {
		t.Errorf("Must contain 'foo_dir' in %s", output)
	}
	if !strings.Contains(output, "foo_file") {
		t.Errorf("Must contain 'foo_file' in %s", output)
	}
}
Find just files and directories
package main
import (
	"bytes"
	"log"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)
var benchmarkTestDir = ""
func randString(length int) string {
	now := time.Now()
	rand.Seed(now.UnixNano())
	res := ""
	maxLen := len(alphaNum)
	for i := 0; i <= length; i++ {
		res = res + string(alphaNum[rand.Intn(maxLen)])
	}
	return res
}
func createFiles(directory string, level, maxLevel int) {
	if level == maxLevel {
		return
	}
	for i := 0; i <= 10; i++ {
		filename := filepath.Join(directory, randString(6))
		if i%2 == 0 {  
			os.Create(filename)
		} else {  
			if err := os.Mkdir(filename, 0777); err == nil {
				createFiles(filename, level+1, maxLevel)
			}
		}
	}
}
func createTestDirs(levels int) string {
	targetDir := createTempDir()
	createFiles(targetDir, 0, levels)
	return targetDir
}
func createTempDir() string {
	defaultTmpDir := os.TempDir()
	targetDir := filepath.Join(defaultTmpDir, randString(6))
	if err := os.Mkdir(targetDir, 0777); err != nil {
		log.Fatal(err)
	}
	return targetDir
}
func TestUnixRegexp(t *testing.T) {
	var patterns = map[string]string{
		"test*":  "test.*",
		"test.*": "test\\..*",
		"*.png":  ".*\\.png",
		"a?":     "a.",
		"hello-": "hello\\-",
	}
	for pattern, expected := range patterns {
		result := unixRegexp(pattern)
		if result != expected {
			t.Errorf("Got %s expected %s", result, expected)
		}
	}
}
func TestFindSimple(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "hello"))
	os.Create(filepath.Join(dir, "world"))
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   "hell",
		"directory": dir,
	}, outputBuf)
	expected := "hello"
	result := outputBuf.Bytes()
	if !bytes.Contains(result, []byte(expected)) {
		t.Errorf("Got %s expected %s", result, expected)
	}
}
func TestFindLevels(t *testing.T) {
	levels := 3
	testDir := createTestDirs(levels)
	defer os.RemoveAll(testDir)
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   "",
		"directory": testDir,
	}, outputBuf)
	lines := strings.Split(outputBuf.String(), "\n")
	result := 0
	for _, line := range lines {
		slices := strings.Split(line, testDir)
		if len(slices) > 1 {
			parts := strings.Split(slices[1],
				string(os.PathSeparator))
			if len(parts) > result {
				result = len(parts)
			}
			if len(parts) == levels {  
				return
			}
		}
	}
	t.Errorf("Expected %d levels but only got %d", levels, result)
}
func TestFindUnixPatternQue(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "foo"))
	os.Create(filepath.Join(dir, "baar"))
	os.Create(filepath.Join(dir, "food"))
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   unixRegexp("fo?d"),
		"directory": dir,
	}, outputBuf)
	expected := "food\n"
	parts := strings.Split(outputBuf.String(), string(os.PathSeparator))
	result := parts[len(parts)-1]
	if result != expected {
		t.Errorf("Got %s expected %s", result, expected)
	}
}
func TestFindUnixPatternStar(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "foo"))
	os.Create(filepath.Join(dir, "baar"))
	os.Create(filepath.Join(dir, "food"))
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   unixRegexp("foo*"),
		"directory": dir,
	}, outputBuf)
	output := outputBuf.String()
	if !(strings.Contains(output, "foo\n") &&
		strings.Contains(output, "food\n")) {
		t.Errorf("Didn't get foo and food")
	}
}
func TestFindRegexp(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "faroo"))
	os.Create(filepath.Join(dir, "bar"))
	os.Create(filepath.Join(dir, "food"))
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":   "ar$",
		"directory": dir,
	}, outputBuf)
	if !strings.Contains(outputBuf.String(), "bar\n") {
		t.Errorf("Didn't get foo and food")
	}
}
func TestFindCaseSensitive(t *testing.T) {
	dir := createTempDir()
	defer os.RemoveAll(dir)
	os.Create(filepath.Join(dir, "Foo"))
	os.Create(filepath.Join(dir, "foo"))
	outputBuf := bytes.NewBufferString("")
	Find(map[string]string{
		"pattern":       "Foo",
		"directory":     dir,
		"caseSensitive": "true",
	}, outputBuf)
	output := outputBuf.String()
	if !strings.Contains(output, "Foo") {
		t.Errorf("Didn't find 'Foo' in %s", output)
	}
	if strings.Contains(output, "foo") {
		t.Errorf("Shouldn't get 'foo' in %s", output)
	}
}