【发布时间】:2021-09-07 14:34:32
【问题描述】:
我正在为用 Go 编写的文件管理器构建一个后端。
我正在寻找一种方法来制作示例切片字符串路径。
sliceStr := []string{
"/file_A.txt",
"/folder_B/file_B.txt",
"/folder_C/sub_folder_C/file_C.txt",
"/folder_C/sub_folder_C/file_C_2.txt",
}
创建一个返回分层 json 结构的 api,以使用 Devextreme FileManager 传递给前端。如下所示
[
{
"name": "file_A.txt",
"isDirectory": false
},
{
"name": "folder_B",
"isDirectory": true,
"items": [
{
"name": "file_B.txt",
"isDirectory": false
}
]
},
{
"name": "folder_C",
"isDirectory": true,
"items": [
{
"name": "sub_folder_C",
"isDirectory": true,
"items": [
{
"name": "file_C.txt",
"isDirectory": false,
},
{
"name": "file_C_2.txt",
"isDirectory": false
}
]
}
]
}
]
【问题讨论】:
-
我写了一个小模块,基本上就是这样做的。但它使用点作为分隔符而不是斜线。 github.com/bluebrown/labelparser。您可以根据自己的喜好对其进行修改。也许它有助于理解这个想法。