x_dirtree

package
v0.0.0-...-0df5ea9 Latest Latest
Warning

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

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

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 节点名称
	Name string

	//node base name does not contain extension 节点名称 不包含扩展名
	BaseName string

	//node extension 节点扩展名
	ExtName string

	//current dir name 当前节点所在文件夹名称
	CurrDirName string

	//base dir 当前节点所在文件夹路径地址 不包含当前节点所在文件夹
	BaseDirPath string

	//is dir 是否文件夹
	IsDir bool

	//full path 完整路径
	FullPath string

	//file info os.DirEntry.Info()
	FileInfo os.FileInfo

	//file mode os.DirEntry.Type()
	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

Get directory tree recursively 递归获取目录树

param: name dir path 目录路径地址

param: matchExt match extension map nil all files 匹配扩展名Map

param: sortFunc sort function nil no sort 排序函数

return:

Example
package main

import (
	"fmt"
	"github.com/guolei19850528/go_x_pkgs/x_dir/x_dirtree"
	"sort"
	"strconv"
)

func main() {
	name := "/Users/yourname/images"
	matchExt := map[string]string{
		".png": ".png",
	}
	sortFunc := func(dirTreeNodes []*x_dirtree.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 := x_dirtree.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