godirtree

package module
v0.0.0-...-77a907f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 4 Imported by: 0

README

godirtree

a golang dir tree library

install

go get github.com/guolei19850528/godirtree

example:

package main

import (
	"fmt"
	"github.com/guolei19850528/godirtree"
	"sort"
	"strconv"
)

func main() {
	name := "/Users/yourname/images"
	matchExt := map[string]string{
		".png": ".png",
	}
	sortFunc := func(dirTreeNodes []*godirtree.TreeNode) {
		sort.Slice(dirTreeNodes, func(i, j int) bool {
			sortKey1, err1 := strconv.ParseInt(dirTreeNodes[i].BaseName, 10, 32)
			sortKey2, err2 := strconv.ParseInt(dirTreeNodes[j].BaseName, 10, 32)
			if err1 == nil && err2 == nil {
				return sortKey1 < sortKey2
			}
			return dirTreeNodes[i].BaseName < dirTreeNodes[j].BaseName
		})
	}

	dirTree := godirtree.Tree(name, matchExt, sortFunc)
	fmt.Println(dirTree)
}


Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TreeNode

type TreeNode struct {
	//node name example:"1.png"
	Name string

	//node base name does not contain extension example:"1"
	BaseName string

	//node extension example:".png"
	ExtName string

	//current dir name example: "a"
	CurrDirName string

	//base dir path example:"/Users/guolei/images"
	BaseDirPath string

	//is dir
	IsDir bool

	//full path example:"/Users/guolei/images/a/1.png"
	FullPath string

	//file info
	FileInfo os.FileInfo

	//file mode
	Type fs.FileMode

	//array of subordinate nodes
	Children []*TreeNode
}

TreeNode Directory Tree Node

func Tree

func Tree(name string, matchExt map[string]string, sortFunc func([]*TreeNode)) []*TreeNode

Tree

param: name dir path param: matchExt match extension map param: sortFunc sort function return:

Example
package main

import (
	"fmt"
	"github.com/guolei19850528/godirtree"
	"sort"
	"strconv"
)

func main() {
	name := "/Users/yourname/images"
	matchExt := map[string]string{
		".png": ".png",
	}
	sortFunc := func(dirTreeNodes []*godirtree.TreeNode) {
		sort.Slice(dirTreeNodes, func(i, j int) bool {
			sortKey1, err1 := strconv.ParseInt(dirTreeNodes[i].BaseName, 10, 32)
			sortKey2, err2 := strconv.ParseInt(dirTreeNodes[j].BaseName, 10, 32)
			if err1 == nil && err2 == nil {
				return sortKey1 < sortKey2
			}
			return dirTreeNodes[i].BaseName < dirTreeNodes[j].BaseName
		})
	}

	dirTree := godirtree.Tree(name, matchExt, sortFunc)
	fmt.Println(dirTree)
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL