{"id":207228,"date":"2025-07-08T09:10:00","date_gmt":"2025-07-08T01:10:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207228\/"},"modified":"2025-07-08T09:10:00","modified_gmt":"2025-07-08T01:10:00","slug":"%e4%bb%8e-go-%e4%b8%ad%e5%85%b6%e4%bb%96%e5%8c%85%e4%b8%ad%e7%9a%84%e7%bb%93%e6%9e%84%e4%b8%ad%e8%af%bb%e5%8f%96%e4%b8%8a%e4%b8%8b%e6%96%87%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207228\/","title":{"rendered":"\u4ece Go \u4e2d\u5176\u4ed6\u5305\u4e2d\u7684\u7ed3\u6784\u4e2d\u8bfb\u53d6\u4e0a\u4e0b\u6587\u503c"},"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>\u4ece Go \u4e2d\u5176\u4ed6\u5305\u4e2d\u7684\u7ed3\u6784\u4e2d\u8bfb\u53d6\u4e0a\u4e0b\u6587\u503c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 22:39:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4ece Go \u4e2d\u5176\u4ed6\u5305\u4e2d\u7684\u7ed3\u6784\u4e2d\u8bfb\u53d6\u4e0a\u4e0b\u6587\u503c\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211<strong>\u5728\u7ed3\u6784\u4e2d\u8bbe\u7f6e\u503c<\/strong>\u5e76\u5c06\u8be5\u7ed3\u6784\u4f20\u9012\u5230<strong>\u4e0a\u4e0b\u6587<\/strong>\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>package client\n\ntype (\n    applicationstate struct{}\n)\n\ntype applicationstate struct {\n    requestid         string\n    requestparameters interface{}\n    requestpath       string\n}\n\nappstate := applicationstate{\n        requestid:         \"123\",\n        requestparameters: req,             \/\/ req is an interface{}\n        requestpath:       \"abc\/def\",\n}\n\nnewctx := context.withvalue(ctx, applicationstate{}, appstate) \/\/ new context after passing the value\nreturn handler(newctx, req) \/\/ passing it to the handler, so i can read it in other interceptor<\/pre>\n<p>\u73b0\u5728\uff0c\u5f53\u6211\u5c1d\u8bd5\u4ece\u4e0a\u4e0b\u6587\uff08\u4fee\u6539\u540e\u7684\u4e0a\u4e0b\u6587\uff09<strong>\u5728\u4e0d\u540c\u7684\u5305\u4e2d<\/strong>\u8bfb\u53d6\u8fd9\u4e9b\u503c\u65f6\uff0c\u7ed3\u679c\u662f <code>nil<\/code>\u3002<\/p>\n<pre>package server\ntype (\n    applicationState struct{}\n)\n\nfmt.Println(\"values from context is:\", ctx.Value((applicationState{}))) \/\/ the value is nil<\/pre>\n<p>\u6211\u5f88\u56f0\u60d1\u6211\u5728\u8fd9\u91cc\u9519\u8fc7\u4e86\u4ec0\u4e48\uff1f\u6211\u8ba4\u4e3a\u5728\u4e0a\u4e0b\u6587\u4e2d\u4f20\u9012\u952e\u503c\u5bf9\u53ef\u4ee5\u5728\u8bbe\u7f6e\u503c\u540e\u5728\u4efb\u4f55\u5730\u65b9\u8bfb\u53d6\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6709\u4eba\u53ef\u4ee5\u9a8c\u8bc1\u8fd9\u662f\u5426\u662f\u6b63\u786e\u7684\u65b9\u6cd5\u5417\uff1f<\/p>\n<p>\u6211\u60f3\u6211\u627e\u5230\u4e86\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u53ef\u80fd\u6b63\u786e\u4e5f\u53ef\u80fd\u4e0d\u6b63\u786e\uff0c\u4f46\u5b83\u4e5f\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5305\u3002<\/p>\n<p>\u521b\u5efa\u4e86\u4e00\u4e2a\u5168\u5c40\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u672c\u5730\u7ed3\u6784\u3002<\/p>\n<pre>package client\n\ntype applicationstate struct {\n    requestid         string\n    requestparameters interface{}\n    requestpath       string\n}\n\ntype (\n    appstate struct{} \/\/ global struct\n)\n\nappstate := applicationstate{\n        requestid:         xrid,\n        requestparameters: req,\n        requestpath:       info.fullmethod,\n    }\n\nnewctx := context.withvalue(ctx, appstate{}, appstate)<\/pre>\n<p>\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u5305\u4e2d\uff0c\u5bfc\u5165\u5305\u5ba2\u6237\u7aef\uff0c\u5e76\u6309\u539f\u6837\u83b7\u53d6\u503c\u3002<\/p>\n<pre>package server\n\nimport \"client\"\n\nfmt.Println(ctx.Value(client.AppState{}).(performance.ApplicationState).RequestID) \/\/ using it here<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4ece Go \u4e2d\u5176\u4ed6\u5305\u4e2d\u7684\u7ed3\u6784\u4e2d\u8bfb\u53d6\u4e0a\u4e0b\u6587\u503c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-207228","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207228","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=207228"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207228\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}