{"id":75561,"date":"2024-03-01T15:03:38","date_gmt":"2024-03-01T07:03:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=75561"},"modified":"2024-03-01T15:03:52","modified_gmt":"2024-03-01T07:03:52","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e4%b8%ad%e8%ae%be%e7%bd%ae%e5%92%8c%e4%bd%bf%e7%94%a8%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/75561.html","title":{"rendered":"\u5982\u4f55\u5728Go\u4e2d\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-75562\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/03\/img-ySEsGMT1Mx4qI6yNsTlYQZBi.png?x-oss-process=image\/auto-orient,1\/format,webp\" width=\"600\" height=\"342\" alt=\"\u5982\u4f55\u5728Go\u4e2d\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\" \/><\/p>\n<p>\u5728<strong>Go\u8bed\u8a00<\/strong>\u4e2d\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8fc7\u7a0b\u3002\u9996\u5148\u9700\u8981\u901a\u8fc7`os`\u5305\u7684<strong>`Setenv`<\/strong>\u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u73af\u73af\u5883\u53d8\u91cf\uff0c\u7136\u540e\u53ef\u4ee5\u5229\u7528`Getenv`\u51fd\u6570\u6765\u68c0\u7d22\u5b83\u3002\u8fd9\u5728\u914d\u7f6e\u9879\u76ee\u548c\u7ba1\u7406\u654f\u611f\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u5bc6\u7801\u3001API\u5bc6\u94a5\u7b49\u3002\u73af\u5883\u53d8\u91cf\u5728\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u524d\u5c31\u5e94\u8be5\u8bbe\u7f6e\u597d\uff0c\u5e76\u4e14\u5b83\u4eec\u901a\u5e38\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u914d\u7f6e\u9009\u9879\u3002\u5728Go\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u73af\u5883\u53d8\u91cf\u505a\u51fa\u8bfb\u53d6\u548c\u4fee\u6539\uff0c\u4ee5\u6ee1\u8db3\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u9700\u6c42\u3002<strong>`os.Getenv`<\/strong>\u51fd\u6570\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u5230\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u800c<strong>`os.Setenv`<\/strong>\u5219\u7528\u4e8e\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u5c55\u5f00\u6765\u8bb2\uff0c`os.Getenv`\u51fd\u6570\u7684\u4f7f\u7528\u7279\u522b\u5e7f\u6cdb\uff0c\u5b83\u51e0\u4e4e\u662f\u6bcf\u4e2a\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7684Go\u7a0b\u5e8f\u7684\u6807\u914d\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u90a3\u4e9b\u53ea\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8bfb\u53d6\u4e00\u6b21\u7684\u914d\u7f6e\u3002<\/p>\n<h2><strong>\u4e00\u3001\u73af\u5883\u53d8\u91cf\u7684\u91cd\u8981\u6027<\/strong><\/h2>\n<p>\u73af\u5883\u53d8\u91cf\u7528\u4e8e\u5b9a\u4e49\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u64cd\u4f5c\u73af\u5883\uff0c\u5b83\u4eec\u662f\u5728\u7cfb\u7edf\u73af\u5883\u4e2d\u8bbe\u7f6e\u7684\u952e\u503c\u5bf9\u3002\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u83b7\u53d6\u5173\u952e\u914d\u7f6e\u4fe1\u606f\uff0c\u800c\u65e0\u9700\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u8fd9\u4e9b\u6570\u636e\u3002\u6b64\u505a\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<h2><strong>\u4e8c\u3001\u5728Go\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong>\u901a\u5e38\u5728\u7a0b\u5e8f\u7684\u521d\u59cb\u5316\u9636\u6bb5\u8fdb\u884c\u3002\u4f7f\u7528`os.Setenv`\u51fd\u6570\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p>&#8220;`go<\/p>\n<p>import &#8220;os&#8221;<\/p>\n<p>func m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n<p>    err := os.Setenv(&#8220;MY_ENV&#8221;, &#8220;my_value&#8221;)<\/p>\n<p>    if err != nil {<\/p>\n<p>        \/\/ \u5904\u7406\u9519\u8bef<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<h2><strong>\u4e09\u3001\u5728Go\u4e2d\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>\u4e00\u65e6\u8bbe\u7f6e\u4e86\u73af\u5883\u53d8\u91cf\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7`os.Getenv`\u51fd\u6570\u6765\u8bbf\u95ee\u5b83\u3002\u5982\u679c\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c`Getenv`\u5c06\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>&#8220;`go<\/p>\n<p>import &#8220;os&#8221;<\/p>\n<p>func main() {<\/p>\n<p>    \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/p>\n<p>    myEnvValue := os.Getenv(&#8220;MY_ENV&#8221;)<\/p>\n<p>    \/\/ \u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7684\u503c<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<h2><strong>\u56db\u3001Go\u4e2d\u73af\u5883\u53d8\u91cf\u7684\u8fdb\u9636\u4f7f\u7528<\/strong><\/h2>\n<p>\u8fdb\u9636\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u662f\u5426\u5b58\u5728\uff0c\u6216\u8005\u5728\u53d8\u91cf\u4e0d\u5b58\u5728\u65f6\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002`os.LookupEnv`\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u73af\u5883\u53d8\u91cf\u7684\u503c\u548c\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u53d8\u91cf\u662f\u5426\u5b58\u5728\u3002<\/p>\n<h2><strong>\u4e94\u3001\u7ba1\u7406\u5e94\u7528\u914d\u7f6e<\/strong><\/h2>\n<p>\u5728\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u5916\u90e8\u5e93\u5982`viper`\u6765\u66f4\u7075\u6d3b\u5730\u7ba1\u7406\u914d\u7f6e\u3002\u8fd9\u4e9b\u5e93\u5141\u8bb8\u4ece\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u548c\u5176\u4ed6\u6570\u636e\u6e90\u8bfb\u53d6\u914d\u7f6e\u3002<\/p>\n<h2><strong>\u516d\u3001\u73af\u5883\u53d8\u91cf\u4e0e\u5e94\u7528\u5b89\u5168<\/strong><\/h2>\n<p>\u5c06\u654f\u611f\u6570\u636e\u5b58\u653e\u5728\u73af\u5883\u53d8\u91cf\u4e2d\u53ef\u4ee5\u51cf\u5c11\u5c06\u5176\u786c\u7f16\u7801\u5728\u6e90\u4ee3\u7801\u4e2d\u7684\u98ce\u9669\u3002\u4e0d\u8fc7\u4e5f\u5e94\u5f53\u8c28\u614e\uff0c\u786e\u4fdd\u4e0d\u8981\u5728\u65e5\u5fd7\u6587\u4ef6\u7b49\u8f93\u51fa\u4e2d\u6cc4\u9732\u8fd9\u4e9b\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<h2><strong>\u4e03\u3001\u5c0f\u7ed3<\/strong><\/h2>\n<p>\u73af\u5883\u53d8\u91cf\u5728Go\u4e2d\u7684\u5e94\u7528\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u5b89\u5168\u3002\u901a\u8fc7\u638c\u63e1`os`\u5305\u63d0\u4f9b\u7684\u73af\u5883\u53d8\u91cf\u76f8\u5173\u51fd\u6570\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e94\u7528\u66f4\u52a0\u5065\u58ee\u5e76\u6613\u4e8e\u7ba1\u7406\u3002\u5728\u5904\u7406\u73af\u5883\u53d8\u91cf\u65f6\uff0c\u52a1\u5fc5\u8c28\u614e\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u654f\u611f\u4fe1\u606f\u65f6\u3002<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u83b7\u53d6\u548c\u64cd\u4f5c\u73af\u5883\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528`os`\u5305\u63d0\u4f9b\u7684\u51fd\u6570\u6765\u83b7\u53d6\u548c\u64cd\u4f5c\u73af\u5883\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7`os.Getenv()`\u51fd\u6570\u83b7\u53d6\u6307\u5b9a\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u901a\u8fc7`os.Setenv()`\u51fd\u6570\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528`os.Unsetenv()`\u51fd\u6570\u5220\u9664\u6307\u5b9a\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Go\u4e2d\u83b7\u53d6\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528`os.Environ()`\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\u7684\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5207\u7247\uff0c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u6bcf\u4e2a\u73af\u5883\u53d8\u91cf\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>\u73af\u5883\u53d8\u91cf\u5728Go\u4e2d\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u73af\u5883\u53d8\u91cf\u5728Go\u8bed\u8a00\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u53c2\u6570\u548c\u914d\u7f6e\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u5177\u6709\u7075\u6d3b\u6027\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u907f\u514d\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u4e00\u4e9b\u673a\u5bc6\u4fe1\u606f\u3002\u5728Go\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u73af\u5883\u53d8\u91cf\u7684\u503c\u505a\u51fa\u4e0d\u540c\u7684\u5904\u7406\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Go\u8bed\u8a00\u4e2d\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8fc7\u7a0b\u3002\u9996\u5148\u9700\u8981\u901a\u8fc7`os`\u5305\u7684`Setenv`\u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u73af\u73af\u5883\u53d8 [&hellip;]","protected":false},"author":1,"featured_media":75564,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75561"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=75561"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75561\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/75564"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=75561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=75561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=75561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}