{"id":207081,"date":"2025-07-08T08:29:49","date_gmt":"2025-07-08T00:29:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207081\/"},"modified":"2025-07-08T08:29:49","modified_gmt":"2025-07-08T00:29:49","slug":"%e6%97%a0%e6%b3%95%e5%88%9b%e5%bb%ba-%e8%ae%bf%e9%97%ae%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207081\/","title":{"rendered":"\u65e0\u6cd5\u521b\u5efa\/\u8bbf\u95ee\u6570\u636e\u5e93"},"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>\u65e0\u6cd5\u521b\u5efa\/\u8bbf\u95ee\u6570\u636e\u5e93<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 21:54: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><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u65e0\u6cd5\u521b\u5efa\/\u8bbf\u95ee\u6570\u636e\u5e93\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4e00\u76f4\u5728 python \u4e2d\u4f7f\u7528 mongodb\uff0c\u6ca1\u6709\u51fa\u73b0\u4efb\u4f55\u95ee\u9898\uff0c\u4f46\u6211\u73b0\u5728\u9700\u8981\u5728 go \u4e2d\u6784\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002 \u6211\u5df2\u7ecf\u67e5\u770b\u4e86\u6587\u6863\uff0c\u793a\u4f8b\u5de5\u4f5c\u6b63\u5e38\u3002<\/p>\n<p> \u4f46\u662f\uff0c\u5f53\u6211\u5c1d\u8bd5\u4f7f\u7528\u81ea\u5df1\u7684\u4ee3\u7801\u65f6\uff0c\u4ee3\u7801\u6267\u884c\u65f6\u6ca1\u6709\u9519\u8bef\uff0c\u4f46\u662f\u5f53\u6211\u68c0\u67e5\u6570\u636e\u5e93\uff08\u901a\u8fc7 cli\uff09\u65f6\uff0c\u6211\u770b\u4e0d\u5230\u6570\u636e\u5e93\uff0c\u6ca1\u6709\u96c6\u5408\uff0c\u4e5f\u6ca1\u6709\u6570\u636e\u3002<\/p>\n<p>\u6211\u786e\u4fe1\u6211\u505a\u9519\u4e86\u4ec0\u4e48\uff0c\u4f46\u6211\u65e0\u6cd5\u5728\u8fd9\u4e2a\u5c0f\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u627e\u5230\u5b83\u3002 <\/p>\n<pre>func main() {\n\n    if client, err := mongo.NewClient(options.Client().ApplyURI(\"mongodb:\/\/localhost:27017\")); err == nil {\n        ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)\n        defer client.Disconnect(ctx)\n        if err = client.Connect(ctx); err == nil {\n            os.Exit(0)\n        }\n\n        KeysCol := client.Database(\"yfroot\").Collection(\"KeysCol\")\n\n        mac, e1 := crypto.GenerateRandomBytes(16)\n        key, e2 := crypto.GenerateRandomBytes(16)\n        if e1 != nil || e2 != nil {\n            fmt.Println(\"failed crypto generate\")\n            os.Exit(0)\n        }\n        testKey := dataformats.DeviceKey{\n            Mac: string(mac),\n            Key: key,\n        }\n\n        \/\/ ctx, _ = context.WithTimeout(context.Background(), time.Duration(10)*time.Second)\n        _, err := KeysCol.InsertOne(ctx, testKey)\n        if err != nil {\n            fmt.Println(err)\n            os.Exit(0)\n        }\n\n    } else {\n        fmt.Println(err)\n    }\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u770b\u8fd9\u90e8\u5206\uff1a<\/p>\n<pre>if err = client.connect(ctx); err == nil {\n  os.exit(0)\n}<\/pre>\n<p>\u5982\u679c\u60a8\u80fd\u591f\u8fde\u63a5\u5230\u6211\u4eec\uff08<code>err<\/code> \u662f <code>nil<\/code>\uff09\uff0c\u5219\u60a8\u5c06\u9000\u51fa\u3002<\/p>\n<p>\u60a8\u53ef\u80fd\u6253\u7b97\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>if err = client.Connect(ctx); err != nil {\n  fmt.Println(err)\n  os.Exit(0)\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207081","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207081","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=207081"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207081\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}