{"id":208162,"date":"2025-07-08T16:20:43","date_gmt":"2025-07-08T08:20:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208162\/"},"modified":"2025-07-08T16:20:43","modified_gmt":"2025-07-08T08:20:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e4%b8%ad%e6%9e%84%e5%bb%ba%e7%bb%93%e6%9e%84%e4%bd%93%e7%9a%84%e9%80%92%e5%bd%92%e5%88%87%e7%89%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208162\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u4e2d\u6784\u5efa\u7ed3\u6784\u4f53\u7684\u9012\u5f52\u5207\u7247\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5728 Go \u4e2d\u6784\u5efa\u7ed3\u6784\u4f53\u7684\u9012\u5f52\u5207\u7247\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 18:21:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5728 Go \u4e2d\u6784\u5efa\u7ed3\u6784\u4f53\u7684\u9012\u5f52\u5207\u7247\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u9700\u8981\u4ece\u4e00\u6bb5\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a json \u6709\u6548\u8d1f\u8f7d\u3002\u5b83\u4eec\u4ee3\u8868\u5355\u4e2a\u6587\u4ef6\u7684\u8def\u5f84\u3002\u5b83\u4eec\u9700\u8981\u5408\u5e76\u5230\u4ee3\u8868\u6574\u4e2a\u76ee\u5f55\u7ed3\u6784\u7684\u6700\u7ec8 json \u6709\u6548\u8d1f\u8f7d\u4e2d\u3002<\/p>\n<h3>\u5982\u679c\u5b57\u7b26\u4e32\u5982\u4e0b\uff1a<\/h3>\n<pre>[\n    \"alpha\/file1.json\"\n    \"alpha\/beta\/file2.json\"\n    \"alpha\/beta\/charlie\/file3.json\"\n    \"alpha\/beta\/charlie\/file4.json\"\n    \"delta\/file5.json\n]<\/pre>\n<h3>json \u8d1f\u8f7d\u7ed3\u6784<\/h3>\n<ul>\n<li>\u76ee\u5f55\u7ed3\u6784\u5e94\u5305\u542b\u4e00\u4e2a\u540d\u79f0\uff0c\u4ee5\u53ca\u5b50\u9879\u548c\/\u6216\u6587\u4ef6\u3002<\/li>\n<li>\u5b50\u7ea7\u5c06\u4ee3\u8868\u53e6\u4e00\u4e2a\u76ee\u5f55\u7ed3\u6784\u53ca\u5176\u81ea\u5df1\u7684\u6587\u4ef6<\/li>\n<li>files \u4ee3\u8868\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6<\/li>\n<\/ul>\n<h3>json \u6709\u6548\u8d1f\u8f7d\u5982\u4e0b\uff1a<\/h3>\n<pre>[{\n    \"Name\": \"alpha\",\n    \"Files\": [{\n        \"Name\": \"file1.json\",\n        \"Path\": \"alpha\/file1.json\"\n    }],\n    \"Children\": [{\n        \"Name\": \"beta\",\n        \"Files\": [{\n            \"Name\": \"file2.json\",\n            \"Path\": \"alpha\/beta\/file2.json\"\n        }],\n        \"Children\": [{\n            \"Name\": \"charlie\",\n            \"Files\": [{\n                \"Name\": \"file3.json\",\n                \"Path\": \"alpha\/beta\/charlie\/file3.json\"\n            },\n            {\n                \"Name\": \"file4.json\",\n                \"Path\": \"alpha\/beta\/charlie\/file4.json\"\n            }]\n        }]\n    }]\n},{\n    \"Name\": \"delta\",\n    \"Files\": [\n        {\n            \"Name\": \"file5.json\",\n            \"Path\": \"delta\/file5.json\"\n        }\n    ]\n}]<\/pre>\n<p>\u6211\u4e0d\u77e5\u9053\u5982\u4f55\u786e\u5b9a\u76ee\u5f55\u540d\u79f0\u662f\u5426\u5df2\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u5c06\u6307\u9488\u4f20\u9012\u7ed9\u8be5\u76ee\u5f55\u3002\u76ee\u524d\uff0c\u5b83\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u8def\u5f84\u5411\u57fa\u672c\u5207\u7247\u9644\u52a0\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u6211\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u5f97\u5230\u7684\u3002<\/p>\n<p>https:\/\/play.golang.org\/p\/wonecjle51z<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u8fd4\u56de\u5207\u7247\u4e2d\u73b0\u6709\u6587\u4ef6\u5939\u7684\u7d22\u5f15\u3002\u7136\u540e\u9012\u5f52\u5176\u5b50\u5143\u7d20\u3002<\/p>\n<pre>\/\/ DirectoryStructure stores the directory structure\ntype DirectoryStructure struct {\n    Name     string               `json:\"Name\"`\n    Children []DirectoryStructure `json:\"Children,omitempty\"`\n    Files    []File               `json:\"Files,omitempty\"`\n}\n\n\/\/ File stores the name of file and path to file\ntype File struct {\n    Name string `json:\"Name\"`\n    Path string `json:\"Path\"`\n}\n\nfunc main() {\n    \/\/ Build slice of strings\n    filePaths := []string{}\n    filePaths = append(filePaths, \"alpha\/file1.json\")\n    filePaths = append(filePaths, \"alpha\/beta\/file2.json\")\n    filePaths = append(filePaths, \"alpha\/beta\/charlie\/file3.json\")\n    filePaths = append(filePaths, \"alpha\/beta\/charlie\/file4.json\")\n    filePaths = append(filePaths, \"delta\/file5.json\")\n\n    \/\/ Create directory with slice of structs\n    directory := []DirectoryStructure{}\n\n    \/\/ Iterate through slice\n    for _, path := range filePaths {\n        pathSlice := strings.Split(path, \"\/\")\n        \/\/ Start recursive function. Passing directory and assigning it to itself\n        directory = buildDirectory(pathSlice, path, directory)\n    }\n\n    b, err := json.Marshal(directory)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    fmt.Println(string(b))\n}\n\nfunc buildDirectory(path []string, key string, directory []DirectoryStructure) []DirectoryStructure {\n    \/\/ Return index of folder if it already exists.\n    folderIndex := doesExist(path[0], directory)\n    \/\/ If folder exists and the next value in the path slice is not the file\n    if folderIndex &gt; -1 &amp;&amp; len(path) != 2 {\n        \/\/ Pass existing folder's Children to buildDirectory function and assign to itself\n        directory[folderIndex].Children = buildDirectory(path[1:], key, directory[folderIndex].Children)\n        return directory\n    }\n    \/\/ If next value in the path slice is a file\n    if len(path) == 2 {\n        \/\/ If folder does not exist\n        if folderIndex == -1 {\n            \/\/ Create new folder with files inside and append to directory\n            fileDirectory := DirectoryStructure{}\n            fileDirectory.Files = []File{{path[1], string(key)}}\n            fileDirectory.Name = path[0]\n            directory = append(directory, fileDirectory)\n        } else {\n            \/\/ If folder does exist, append files to the directory\n            directory[folderIndex].Files = append(directory[folderIndex].Files, File{path[1], string(key)})\n        }\n        return directory\n    }\n    \/\/ Folder does not exist and the next value is not a file\n    \/\/ Create new directory, recurse it's Children, and append when returned\n    currentDirectory := DirectoryStructure{}\n    currentDirectory.Name = path[0]\n    currentDirectory.Children = buildDirectory(path[1:], key, []DirectoryStructure{})\n    directory = append(directory, currentDirectory)\n    return directory\n\n}\n\n\/\/ doesExist returns index of folder if exists\nfunc doesExist(folderName string, directory []DirectoryStructure) int {\n    for index, element := range directory {\n        if folderName == element.Name {\n            return index\n        }\n    }\n    return -1\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Go \u4e2d\u6784\u5efa\u7ed3\u6784\u4f53\u7684\u9012\u5f52\u5207\u7247\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208162","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208162"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208162\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}