{"id":207158,"date":"2025-07-08T10:39:55","date_gmt":"2025-07-08T02:39:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207158\/"},"modified":"2025-07-08T10:39:55","modified_gmt":"2025-07-08T02:39:55","slug":"%e5%a5%87%e6%80%aa%e7%9a%84%e5%88%87%e7%89%87%e8%a1%8c%e4%b8%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207158\/","title":{"rendered":"\u5947\u602a\u7684\u5207\u7247\u884c\u4e3a"},"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>\u5947\u602a\u7684\u5207\u7247\u884c\u4e3a<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 14:30:42<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5947\u602a\u7684\u5207\u7247\u884c\u4e3a\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5b9e\u73b0 bfs \u7b97\u6cd5\u6765\u67e5\u627e\u56fe\u4e2d\u7684\u6240\u6709\u8def\u5f84\uff08\u4ece src \u548c dest\uff09\u3002\u6211\u6b63\u5728\u4f7f\u7528\u5207\u7247\u6765\u6a21\u62df\u961f\u5217\uff0c\u4f46\u662f\u5f53\u6211\u5728 for \u5faa\u73af\u4e2d\u5411\u5207\u7247\u8ffd\u52a0\u591a\u4e2a\u5143\u7d20\u65f6\uff0c\u5207\u7247\u4f1a\u88ab\u635f\u574f\uff08\u8ffd\u52a0\u672a\u6309\u9884\u671f\u5de5\u4f5c\uff09\u3002\u6211\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u3002\u6211\u662f goland \u7684\u65b0\u4eba<\/p>\n<pre>\/\/ GetPathsFromCache retrieve information from loaded jsons in the cache\nfunc (cache *ModelsDataCache) GetPathsFromCache(modelUrn, selectedElement, targetType, authToken string, modelJSONs *ModelJSONs) []systemdetection.GraphPath {\n\n    result := make([]systemdetection.GraphPath, 0)\n\n    \/\/create the queue which stores the paths\n    q := make([]Path, 0)\n\n    \/\/Path to store the current path\n    var currentPath Path\n    currentPath.path = append(currentPath.path, selectedElement)\n    q = append(q, currentPath)\n\n\n    for len(q) &gt; 0 {\n\n\n        currentPath = q[0] \/\/get top\n        q = q[1:]\n\n\n        lastInThePath := currentPath.path[len(currentPath.path)-1]\n        connectedToTheCurrent := cache.GetConnectionsFromCache(modelUrn, lastInThePath, authToken, modelJSONs)\n\n        lastInPathType := modelJSONs.Elements[lastInThePath].Type\n\n        if lastInPathType == targetType {\n            \/\/cache.savePath(currentPath, &amp;result)\n\n            var newPath systemdetection.GraphPath\n            newPath.Target = lastInThePath\n            newPath.Path = currentPath.path[:]\n            newPath.Distance = 666\n            result = append(result, newPath)\n        }\n\n\n        for _, connected := range connectedToTheCurrent {\n            if cache.isNotVisited(connected, currentPath.path) {\n\n\n                var newPathN Path\n\n                newPathN.path = currentPath.path\n                newPathN.path = append(newPathN.path, connected)\n\n                q = append(q, newPathN)\n            }\n        }\n\n    }\n\n    return result\n\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u80fd\u6ca1\u6709\u6b63\u786e\u4f7f\u7528 <code>make<\/code>\u3002\u5207\u7247\u7684 <code>make<\/code> \u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u957f\u5ea6\u548c\uff08\u53ef\u9009\uff09\u5bb9\u91cf\uff1a<\/p>\n<pre>make([]t, len, cap)<\/pre>\n<p>len \u662f\u5b83\u5305\u542b\u7684\u5143\u7d20\u7684\u8d77\u59cb\u6570\u91cf\uff0ccapacity \u662f\u5b83\u5728\u4e0d\u6269\u5c55\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5305\u542b\u7684\u5143\u7d20\u6570\u91cf\u3002\u6709\u5173 \u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u89c6\u89c9\u4e0a\uff1a<\/p>\n<pre>make([]string, 5) #=&gt; [\"\", \"\", \"\", \"\", \"\"]\nmake([]string, 0, 5) #=&gt; [] (but the underlying array can hold 5 elements)<\/pre>\n<p><code>append<\/code> \u6dfb\u52a0\u5230\u6570\u7ec4\u7684\u672b\u5c3e\uff0c\u56e0\u6b64\u9075\u5faa\u76f8\u540c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>arr1 := make([]string, 5) #=&gt; [\"\", \"\", \"\", \"\", \"\"]\narr1 = append(arr1, \"foo\") #=&gt; [\"\", \"\", \"\", \"\", \"\", \"foo\"] \n\narr2 := make([]string, 0, 5) #=&gt; []\narr2 = append(arr2, \"foo\") #=&gt; [\"foo\"] (underlying array can hold 4 more elements before expanding)<\/pre>\n<p>\u60a8\u6b63\u5728\u521b\u5efa\u4e00\u5b9a\u957f\u5ea6\u7684\u5207\u7247\uff0c\u7136\u540e\u9644\u52a0\u5230\u672b\u5c3e\uff0c\u8fd9\u610f\u5473\u7740\u5207\u7247\u7684\u524d n \u200b\u200b\u4e2a\u5143\u7d20\u4e3a\u96f6\u503c\u3002\u5c06 <code>make([]t, n)<\/code> \u66f4\u6539\u4e3a <code>make([]t, 0, n)<\/code>\u3002<\/p>\n<p>\u8fd9\u662f\u663e\u793a\u5dee\u5f02\u7684 go \u6f14\u793a\u7684\u94fe\u63a5\uff1a<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5947\u602a\u7684\u5207\u7247\u884c\u4e3a\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\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-207158","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207158","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=207158"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207158\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}