{"record":{"id":"28803473b53d40bb","repo":"krahets/hello-algo","slug":"error-288034","errorCode":null,"errorMessage":"error","messagePattern":"error","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"zh-hant/codes/go/chapter_graph/graph_adjacency_list.go","lineNumber":45,"sourceCode":"\tfor _, edge := range edges {\n\t\tg.addVertex(edge[0])\n\t\tg.addVertex(edge[1])\n\t\tg.addEdge(edge[0], edge[1])\n\t}\n\treturn g\n}\n\n/* 獲取頂點數量 */\nfunc (g *graphAdjList) size() int {\n\treturn len(g.adjList)\n}\n\n/* 新增邊 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n\t_, ok1 := g.adjList[vet1]\n\t_, ok2 := g.adjList[vet2]\n\tif !ok1 || !ok2 || vet1 == vet2 {\n\t\tpanic(\"error\")\n\t}\n\t// 新增邊 vet1 - vet2, 新增匿名 struct{},\n\tg.adjList[vet1] = append(g.adjList[vet1], vet2)\n\tg.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* 刪除邊 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n\t_, ok1 := g.adjList[vet1]\n\t_, ok2 := g.adjList[vet2]\n\tif !ok1 || !ok2 || vet1 == vet2 {\n\t\tpanic(\"error\")\n\t}\n\t// 刪除邊 vet1 - vet2\n\tg.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n\tg.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/go/chapter_graph/graph_adjacency_list.go#L27-L63","documentation":"Panic thrown by addEdge(vet1, vet2) on the undirected adjacency-list graph (Traditional Chinese build) when the edge is illegal: an endpoint vertex is not in the adjacency map, or vet1 == vet2 (no self-loops). The graph only connects two distinct, already-registered vertices, so it aborts with 'error' otherwise.","triggerScenarios":"addEdge() before both addVertex() calls; vet1 == vet2; referencing a vertex removed by removeVertex().","commonSituations":"Loading an edge list without first extracting/registering the vertex set; assuming the graph constructor created vertices it actually skipped; pipeline that deletes a vertex then re-adds an edge touching it.","solutions":["Register both endpoints via addVertex() before addEdge().","Guard: ensure vet1 != vet2 and both vertices are present in the map.","Two-pass graph construction: collect distinct vertices, add them, then add edges.","Drop or log edges referencing unknown/duplicate vertices instead of panicking."],"exampleFix":"// before: panics on missing vertex or self-loop\ng.addEdge(a, b)\n\n// after\nif a != b {\n    g.addVertex(a); g.addVertex(b)\n    g.addEdge(a, b)\n}","handlingStrategy":"validation","validationCode":"if vet1 != vet2 && g.hasVertex(vet1) && g.hasVertex(vet2) {\n    g.addEdge(vet1, vet2)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        // addEdge rejected\n    }\n}()\ng.addEdge(vet1, vet2)","preventionTips":["Register all distinct vertices before adding any edge.","Filter out self-loops (vet1 == vet2) before calling addEdge().","Use two-pass construction: vertices first, edges second."],"tags":["go","graph","adjacency-list","addedge","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}