{"id":207209,"date":"2025-07-08T08:47:18","date_gmt":"2025-07-08T00:47:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207209\/"},"modified":"2025-07-08T08:47:18","modified_gmt":"2025-07-08T00:47:18","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e7%9a%84%e6%96%87%e6%9c%ac-%e6%89%ab%e6%8f%8f%e4%bb%aa%e4%b8%ad%e5%80%92%e5%9b%9e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207209\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u7684\u6587\u672c\/\u626b\u63cf\u4eea\u4e2d\u5012\u56de\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 \u7684\u6587\u672c\/\u626b\u63cf\u4eea\u4e2d\u5012\u56de\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 20:18: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>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Go \u7684\u6587\u672c\/\u626b\u63cf\u4eea\u4e2d\u5012\u56de\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 <code>text\/scanner<\/code> \u5305\u6765\u89e3\u6790\u4e00\u4e9b\u4efb\u610f\u8868\u8fbe\u5f0f\u3002\u6211\u76ee\u524d\u6b63\u5728\u5c1d\u8bd5\u5728\u9009\u9879\u4e2d\u5b9e\u73b0<code>not\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u5f53\u524d\u6807\u8bc6\u7b26\u662f<code>not<\/code>\uff0c\u4e0b\u4e00\u4e2a\u662f<code>in<\/code>\uff0c\u5219\u4f7f\u7528\u51fd\u6570<code>notin(left, right)<\/code>\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\uff0c\u5426\u5219\u6211\u4eec\u5c06\u5176\u89e3\u6790\u4e3a<code>negate(right)<\/code> .<\/code><\/p>\n<p>\u6211\u57fa\u672c\u4e0a\u5df2\u7ecf\u83b7\u5f97\u4e86\u7ba1\u7406\u8fd9\u4e9b\u60c5\u51b5\u7684\u4ee3\u7801\uff0c\u4f46\u662f\uff0c\u5982\u679c\u4e0b\u4e00\u4e2a\u6807\u8bb0\u4e0d\u662f <code>in<\/code>\uff0c\u6211\u65e0\u6cd5\u5012\u5e26\u626b\u63cf\u4eea\u3002\u6211\u5c1d\u8bd5\u8fc7\u8bb0\u5f55\u4f4d\u7f6e\uff0c\u7136\u540e\u7a0d\u540e\u91cd\u65b0\u5206\u914d\uff0c\u4f46\u65e0\u6d4e\u4e8e\u4e8b\uff0c\u5e76\u4e14\u65e0\u6cd5\u627e\u5230\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<pre>func readtoken(stream *scanner.scanner) {\n    switch stream.scan() {\n    case scanner.ident:\n        switch stream.tokentext() {\n        case \"in\":\n            in(left, right)\n        case \"not\":\n            oldposition := stream.position\n            nexttoken := stream.scan()\n            if nexttoken == scanner.ident {\n                switch stream.tokentext() {\n                case \"in\":\n                    fmt.println(\"notin\")\n                default:\n                    \/\/ how do we rewind the scanner?\n                    stream.position = oldposition \n                    fmt.println(\"negate default\")\n                }\n            } else {\n                fmt.println(\"negate no-ident\")\n            }\n        }\n    }\n}\n<\/pre>\n<p>\u5f53\u6211\u627e\u4e0d\u5230\u6709\u6548\u7684\u6807\u8bc6\u7b26\u65f6\uff0c\u5982\u4f55\u5012\u56de\u626b\u63cf\u4eea\uff1f<\/p>\n<p>\u7f16\u8f91\uff0c\u6211\u8fd8\u5c1d\u8bd5\u4f7f\u7528 <code>peek()<\/code> \u5982\u4e0b\uff0c\u4f46\u8fd9\u4ecd\u7136\u5c06\u72b6\u6001\u66f4\u6539\u4e3a\u6211\u4e5f\u9700\u8981\u5012\u5e26\u7684\u7a0b\u5ea6\u3002<\/p>\n<pre>\/\/ other code\ncase \"not\":\n    nextIdent, err := getNextIdent(stream)\n    if err != nil {\n        fmt.Println(\"negate no-ident\")\n    } else {\n        switch nextIdent {\n        case \"in\":\n            fmt.Println(\"notin\")\n        default:\n            fmt.Println(\"negate default\")\n        }\n    }\n\/\/ other code\n\n\nfunc getNextIdent(s *scanner.Scanner) (string, error) {\n    var nextIdent string\n\n    ch := s.Peek()\n\n    \/\/ skip white space\n    for s.Whitespace&amp;(1&lt;&lt;uint(ch)) != 0 {\n        ch = s.Next()\n    }\n\n    if isIdentRune(ch, 0) {\n        nextIdent = string(ch)\n        ch = s.Next()\n        nextIdent += string(ch)\n        for i := 1; isIdentRune(ch, i); i++ {\n            ch = s.Next()\n            if s.Whitespace&amp;(1&lt;&lt;uint(ch)) != 0 {\n                break\n            }\n            nextIdent += string(ch)\n        }\n        return nextIdent, nil\n    }\n\n    return \"\",errors.New(\"not a ident\")\n}\n<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6211\u5f97\u5230\u7684\u4ee3\u7801\u662f\u6765\u81ea knetic\/govaluate \u7684\u5206\u53c9\uff0c\u7ed3\u5408\u4e86 gh \u7528\u6237generikvault \u7684 pr \u548c\u5176\u4ed6\u4e00\u4e9b\u5206\u53c9\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u53ef\u4ee5\u5728\u6211\u7684 github \u4e2a\u4eba\u8d44\u6599\u4e2d\u627e\u5230<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u901a\u8fc7\u67e5\u770b <code>text\/scanner<\/code> \u7684 API \u53c2\u8003\uff0c\u6211\u4f3c\u4e4e\u627e\u4e0d\u5230\u4e00\u79cd\u6309\u7167\u60a8\u60f3\u8981\u7684\u65b9\u5f0f\u5012\u5e26\u626b\u63cf\u4eea\u7684\u65b9\u6cd5\u3002 <\/p>\n<p>\u4f46\u662f\uff0c<code>Peek()<\/code> \u65b9\u6cd5\u5c06\u5728\u4e0d\u63a8\u8fdb\u626b\u63cf\u4eea\u7684\u60c5\u51b5\u4e0b\u4e3a\u60a8\u63d0\u4f9b\u4e0b\u4e00\u4e2a\u7b26\u6587\u3002\u5728\u201cnot\u201d\u7684\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u63d0\u524d\u67e5\u770b\u662f\u5426\u5339\u914d\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5728 Go \u7684\u6587\u672c\/\u626b\u63cf\u4eea\u4e2d\u5012\u56de\uff1f\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-207209","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207209","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=207209"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207209\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}