{"record":{"id":"6e79b433974b1fdc","repo":"krahets/hello-algo","slug":"error-6e79b4","errorCode":null,"errorMessage":"error","messagePattern":"error","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"en/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/* Get the number of vertices */\nfunc (g *graphAdjList) size() int {\n\treturn len(g.adjList)\n}\n\n/* Add edge */\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// Add edge vet1 - vet2, add anonymous struct{},\n\tg.adjList[vet1] = append(g.adjList[vet1], vet2)\n\tg.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* Remove edge */\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// Remove edge 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/en/codes/go/chapter_graph/graph_adjacency_list.go#L27-L63","documentation":"`addEdge(vet1, vet2)` in the English `graphAdjList` panics with \"error\" when an endpoint is absent from the adjacency map or the two endpoints are the same. The undirected model requires both vertices pre-registered and disallows self-loops.","triggerScenarios":"`addEdge` on a vertex not added via `addVertex`; `addEdge(v, v)`; constructing an edge before its vertices.","commonSituations":"Edge lists loaded without the vertex set, self-loop entries in data, or a typo making one endpoint unknown.","solutions":["Add both vertices with `addVertex` first.","Drop self-loops from input (`vet1 != vet2`).","Assert existence via a `hasVertex` helper.","Recover when ingesting untrusted edge data."],"exampleFix":"// before\ng.addEdge(a, b) // panic if missing endpoint or a == b\n\n// after\nif hasVertex(g, a) && hasVertex(g, b) && a != b {\n    g.addEdge(a, b)\n}","handlingStrategy":"validation","validationCode":"if !hasVertex(g, vet1) || !hasVertex(g, vet2) || vet1 == vet2 {\n    return errors.New(\"endpoints missing or self-loop\")\n}\ng.addEdge(vet1, vet2)","typeGuard":"func hasVertex(g *graphAdjList, v Vertex) bool {\n    _, ok := g.adjList[v]\n    return ok\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        // skip the bad edge during bulk ingestion\n    }\n}()\ng.addEdge(vet1, vet2)","preventionTips":["addVertex for both endpoints before addEdge.","Strip self-loops and unknown vertices from imported data.","Wrap ingestion in recover so one record can't abort the batch."],"tags":["go","graph","adjacency-list","panic","teaching-example"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}