{"id":207436,"date":"2025-07-08T13:23:27","date_gmt":"2025-07-08T05:23:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207436\/"},"modified":"2025-07-08T13:23:27","modified_gmt":"2025-07-08T05:23:27","slug":"ast-inspect-%e4%b8%8d%e8%b5%b0-ast-unaryexpr","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207436\/","title":{"rendered":"ast.Inspect \u4e0d\u8d70 *ast.UnaryExpr"},"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>ast.Inspect \u4e0d\u8d70 *ast.UnaryExpr<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 16:30:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aast.Inspect \u4e0d\u8d70 *ast.UnaryExpr\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u68c0\u67e5 go \u6e90\u4ee3\u7801\u4ee5\u5236\u4f5c\u4e00\u4e2a\u5de5\u5177\u3002\u4e3a\u6b64\uff0c\u6211\u4f7f\u7528 <code>ast.inspect<\/code> \u51fd\u6570\u3002<\/p>\n<p>\u6211\u9700\u8981\u77e5\u9053\u51fd\u6570\/\u65b9\u6cd5\u5185\u90e8\u5982\u4f55\u4f7f\u7528\u901a\u9053\u3002 <\/p>\n<p>\u6211\u5c06\u6b64\u4f5c\u4e3a\u8981\u68c0\u67e5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nfunc b(ch chan int) {\n    for x := range ch {\n\n    }\n}\n<\/pre>\n<p>\u8fd9\u662f\u51fd\u6570 b \u7684 ast\uff1a<\/p>\n<pre>0  *ast.funcdecl {\n     1  .  name: *ast.ident {\n     2  .  .  namepos: -:4:6\n     3  .  .  name: \"b\"\n     4  .  .  obj: *ast.object {\n     5  .  .  .  kind: func\n     6  .  .  .  name: \"b\"\n     7  .  .  .  decl: *(obj @ 0)\n     8  .  .  }\n     9  .  }\n    10  .  type: *ast.functype {\n    11  .  .  func: -:4:1\n    12  .  .  params: *ast.fieldlist {\n    13  .  .  .  opening: -:4:7\n    14  .  .  .  list: []*ast.field (len = 1) {\n    15  .  .  .  .  0: *ast.field {\n    16  .  .  .  .  .  names: []*ast.ident (len = 1) {\n    17  .  .  .  .  .  .  0: *ast.ident {\n    18  .  .  .  .  .  .  .  namepos: -:4:8\n    19  .  .  .  .  .  .  .  name: \"ch\"\n    20  .  .  .  .  .  .  .  obj: *ast.object {\n    21  .  .  .  .  .  .  .  .  kind: var\n    22  .  .  .  .  .  .  .  .  name: \"ch\"\n    23  .  .  .  .  .  .  .  .  decl: *(obj @ 15)\n    24  .  .  .  .  .  .  .  }\n    25  .  .  .  .  .  .  }\n    26  .  .  .  .  .  }\n    27  .  .  .  .  .  type: *ast.chantype {\n    28  .  .  .  .  .  .  begin: -:4:11\n    29  .  .  .  .  .  .  arrow: -\n    30  .  .  .  .  .  .  dir: 3\n    31  .  .  .  .  .  .  value: *ast.ident {\n    32  .  .  .  .  .  .  .  namepos: -:4:16\n    33  .  .  .  .  .  .  .  name: \"int\"\n    34  .  .  .  .  .  .  }\n    35  .  .  .  .  .  }\n    36  .  .  .  .  }\n    37  .  .  .  }\n    38  .  .  .  closing: -:4:19\n    39  .  .  }\n    40  .  }\n    41  .  body: *ast.blockstmt {\n    42  .  .  lbrace: -:4:21\n    43  .  .  list: []ast.stmt (len = 1) {\n    44  .  .  .  0: *ast.rangestmt {\n    45  .  .  .  .  for: -:5:2\n    46  .  .  .  .  key: *ast.ident {\n    47  .  .  .  .  .  namepos: -:5:6\n    48  .  .  .  .  .  name: \"x\"\n    49  .  .  .  .  .  obj: *ast.object {\n    50  .  .  .  .  .  .  kind: var\n    51  .  .  .  .  .  .  name: \"x\"\n    52  .  .  .  .  .  .  decl: *ast.assignstmt {\n    53  .  .  .  .  .  .  .  lhs: []ast.expr (len = 1) {\n    54  .  .  .  .  .  .  .  .  0: *(obj @ 46)\n    55  .  .  .  .  .  .  .  }\n    56  .  .  .  .  .  .  .  tokpos: -:5:8\n    57  .  .  .  .  .  .  .  tok: :=\n    58  .  .  .  .  .  .  .  rhs: []ast.expr (len = 1) {\n    59  .  .  .  .  .  .  .  .  0: *ast.unaryexpr {\n    60  .  .  .  .  .  .  .  .  .  oppos: -:5:11\n    61  .  .  .  .  .  .  .  .  .  op: range\n    62  .  .  .  .  .  .  .  .  .  x: *ast.ident {\n    63  .  .  .  .  .  .  .  .  .  .  namepos: -:5:17\n    64  .  .  .  .  .  .  .  .  .  .  name: \"ch\"\n    65  .  .  .  .  .  .  .  .  .  .  obj: *(obj @ 20)\n    66  .  .  .  .  .  .  .  .  .  }\n    67  .  .  .  .  .  .  .  .  }\n    68  .  .  .  .  .  .  .  }\n    69  .  .  .  .  .  .  }\n    70  .  .  .  .  .  }\n    71  .  .  .  .  }\n    72  .  .  .  .  tokpos: -:5:8\n    73  .  .  .  .  tok: :=\n    74  .  .  .  .  x: *(obj @ 62)\n    75  .  .  .  .  body: *ast.blockstmt {\n    76  .  .  .  .  .  lbrace: -:5:20\n    77  .  .  .  .  .  rbrace: -:7:2\n    78  .  .  .  .  }\n    79  .  .  .  }\n    80  .  .  }\n    81  .  .  rbrace: -:8:1\n    82  .<\/pre>\n<p>\u6b63\u5982\u60a8\u5728 <code>59<\/code> \u884c\u4e2d\u770b\u5230\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6709\u4e00\u4e2a <code>unaryexpr<\/code> \u8282\u70b9\uff0c\u5176\u4e2d <code>op<\/code> \u8bbe\u7f6e\u4e3a <code>range<\/code>\u3002\u8fd9\u5c31\u662f\u6211\u60f3\u8981\u6355\u83b7\u7684\u8282\u70b9\u3002<\/p>\n<p>\u6211\u5c1d\u8bd5\u4f7f\u7528\u6b64\u4ee3\u7801\u904d\u5386 ast \u5e76\u4e14\u4ec5\u6355\u83b7\u8be5\u8282\u70b9\u3002<\/p>\n<pre>exampleFunc := `\npackage main\n\nfunc B(ch chan int) {\n    for x := range ch {\n\n    }\n}\n`\nfset := token.NewFileSet()\nfile, err := parser.ParseFile(fset, \"-\", exampleFunc, parser.ParseComments)\nast.Inspect(file, func(node ast.Node) bool {\n    fn, ok := node.(*ast.UnaryExpr) \/\/ try to cast\n    if !ok {\n        return true\n    }\n        ast.Print(fset, fn)\n    return true\n})\n<\/pre>\n<p>\u4f46\u4f3c\u4e4e\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p>\u77e5\u9053\u4e3a\u4ec0\u4e48\uff0c\u6253\u5370\u6574\u4e2a funcdecl \u7684 ast\uff0c\u6211\u53ef\u4ee5\u770b\u5230\u6709\u4e00\u4e2a unaryexpr \u8282\u70b9\uff0c\u4f46\u5728\u5c1d\u8bd5\u83b7\u53d6\u8be5\u8282\u70b9\u65f6\u4ec0\u4e48\u4e5f\u6ca1\u6709\u51fa\u73b0\uff1f <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f5c\u4e3a@mkopriva\uff0c<code>*ast.Ident<\/code> \u8282\u70b9\u88ab\u89c6\u4e3a\u53f6\u5b50\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u4ece <code>RangeStmt<\/code> \u79fb\u52a8\u5230 <code>AssignStmt<\/code> \uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\uff0c\u7136\u540e\u518d\u6b21\u4f7f\u7528\u68c0\u67e5\u6765\u5230\u8fbe <code>UnaryExpr<\/code><\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aast.Inspect \u4e0d\u8d70 *ast.UnaryExpr\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-207436","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207436","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=207436"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207436\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}