{"id":729,"date":"2024-07-09T16:05:28","date_gmt":"2024-07-09T08:05:28","guid":{"rendered":"https:\/\/www.madbull.site\/?p=729"},"modified":"2024-10-14T20:23:48","modified_gmt":"2024-10-14T12:23:48","slug":"golang%e5%ae%9e%e7%8e%b0yaml%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e7%9a%84%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=729","title":{"rendered":"golang\u5b9e\u73b0yaml\u914d\u7f6e\u6587\u4ef6\u7684\u89e3\u6790"},"content":{"rendered":"\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-3-color\"><strong>\u9700\u8981\u5efa\u7acb3\u4e2a\u6587\u4ef6\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/strong><\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"139\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/11.png\" alt=\"\" class=\"wp-image-730\"\/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u914d\u7f6e\u6587\u4ef6 conf.yaml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>redis:  \n  host: \"127.0.0.1\"\n  port: 6379\n  db: 11  \n  pass: \"aaaaaa\" <\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>go\u6a21\u5757\u6587\u4ef6 go.mod\uff0c\u7528\u4e8e\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u7248\u672c\u4fe1\u606f<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module readyaml<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u4e3b\u7a0b\u5e8f readyaml.go<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"github.com\/spf13\/viper\"\n    \"fmt\"\n)\n\n\/\/ \u914d\u7f6e\u6587\u4ef6\ntype Conf struct {\n    Redis RedisConfig      `mapstructure:\"redis\"`\n}\n\n\/\/ redis\u914d\u7f6e\u6570\u636e\ntype RedisConfig struct{\n    HOST    string    `mapstructure:\"host\"`\n    PORT    int       `mapstructure:\"port\"`\n    DB      int       `mapstructure:\"db\"`\n    PASS    string    `mapstructure:\"pass\"`\n}\n\nfunc getConf() Conf{\n    var cfg Conf \n\n    \/\/ \u8bbe\u7f6e\u5f85\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u540d\u5b57\n    viper.SetConfigName(\"conf\")\n    \/\/ \u8bbe\u7f6e\u5f85\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\n    viper.SetConfigType(\"yaml\")\n    \/\/ \u6dfb\u52a0\u914d\u7f6e\u6587\u4ef6\u7684\u68c0\u7d22\u8def\u5f84\n    viper.AddConfigPath(\".\")\n\n    \/\/ \u8bfb\u53d6\u6570\u636e\n    if err := viper.ReadInConfig(); err != nil {\n        fmt.Printf(\"ReadInConfig error : %s\\n\", err)\n        return cfg\n    }\n\n    \/\/ \u6d4b\u8bd5 1 : \u76f4\u63a5\u83b7\u53d6\u6570\u636e\n    fmt.Printf(\"Host %s\\n\", viper.GetString(\"redis.host\"))\n    fmt.Printf(\"PORT %d\\n\", viper.GetInt(\"redis.port\"))\n    fmt.Printf(\"DB %d\\n\", viper.GetInt(\"redis.db\"))\n    fmt.Printf(\"PASS %s\\n\", viper.GetString(\"redis.pass\"))\n\n    \/\/ \u901a\u8fc7viper\u6a21\u5757\uff0c\u53cd\u5e8f\u5217\u5316\u6210\u5bf9\u8c61\n    if err := viper.Unmarshal(&amp;cfg); err != nil {\n        fmt.Printf(\"Unmarshal error : %s\\n\", err)\n        return cfg\n    }\n\n    return cfg \n}\n\nvar cfg Conf = getConf()\n\nfunc main() {\n    \/\/ \u6253\u5370\u6d4b\u8bd5\n    fmt.Printf(\"HOST %s, PORT %d, DB %d, PASS %s len(PASS) %d\\n\", cfg.Redis.HOST, cfg.Redis.PORT, cfg.Redis.DB, cfg.Redis.PASS, len(cfg.Redis.PASS) )\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-3-color\">\u9996\u5148\uff0c\u9700\u8981\u6574\u7406\u548c\u66f4\u65b0\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb<\/mark><\/strong><\/p>\n\n\n\n<p>go mod tidy<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u4e0b\u8f7d\u7f3a\u5931\u7684\u6a21\u5757\u3002go.mod\u6587\u4ef6\u4f1a\u8fdb\u884c\u66f4\u65b0\uff0c\u5e76\u4e14\u591a\u51fa\u4e00\u4e2ago.sum\u6587\u4ef6\uff0c\u7528\u4e8e\u8bb0\u5f55\u9879\u76ee\u4f9d\u8d56\u6811\u4e2d\u6240\u6709\u6a21\u5757\u7684\u7279\u5b9a\u7248\u672c\u5185\u5bb9\u7684\u9884\u671f\u54c8\u5e0c\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"617\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/22.png\" alt=\"\" class=\"wp-image-732\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/22.png 682w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/22-300x271.png 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u65e0\u6cd5\u8054\u7f51\uff0c\u6216\u8005\u65e0\u6cd5\u8bbf\u95ee\u6e90\uff0c\u5219\u4f1a\u62a5\u9519\u5982\u4e0b\u56fe\u3002\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u7b2c\u4e8c\u6b65\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"81\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/33-1024x81.png\" alt=\"\" class=\"wp-image-733\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/33-1024x81.png 1024w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/33-300x24.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/33-768x61.png 768w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/33.png 1346w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u56fd\u5185go\u4ee3\u7406\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6b64\u5904\u7528\u5230GOPROXY\u73af\u5883\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u6267\u884c <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f00d0d\" class=\"has-inline-color\">export GOPROXY=https:\/\/goproxy.cn<\/mark><\/strong>\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c go mod tidy<\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u5728 \/etc\/bashrc\u6216\u8005~\/.bashrc\u6587\u4ef6\u6dfb\u52a0\u8fd9\u4e2a\u547d\u4ee4\uff0c\u8fd9\u6837\u6bcf\u6b21\u767b\u5f55\u4f1a\u8bdd\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6b64\u73af\u5883\u53d8\u91cf\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-3-color\">\u7b2c\u4e8c\u6b65\uff0c\u76f4\u63a5\u6267\u884c\u811a\u672c\uff1a go run readyaml.go ;<\/mark><\/strong><\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u7f16\u8bd1\u540e\u518d\u6267\u884c\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a go build readyaml.go\uff0c\u7f16\u8bd1\u540e\uff0c\u521b\u5efa\u4e86\u7f16\u8bd1\u540e\u7684\u6587\u4ef6 readyaml\u3002<\/p>\n\n\n\n<p>\u76f4\u63a5\u6267\u884c .\/readyaml\u3002\u5177\u4f53\u64cd\u4f5c\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"328\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/44.png\" alt=\"\" class=\"wp-image-735\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/44.png 494w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/44-300x199.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n<\/div>\n\n\n\n<div style=\"height:58px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:clamp(0.984rem, 0.984rem + ((1vw - 0.2rem) * 0.764), 1.5rem);\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ec4141\" class=\"has-inline-color\">\u907f\u5751\uff1a\u5b9a\u4e49\u7684\u6570\u636e\u7ed3\u6784\u6210\u5458\u53d8\u91cf\u5fc5\u987b\u662f\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u4e0d\u7136 viper.Unmarshal \u8fd9\u4e00\u6b65\u53ef\u80fd\u6620\u5c04\u4e0d\u6210\u529f\u3002\u8fd9\u662f\u7531\u4e8e\u5728golang\u4e2d\uff0c\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\uff08\u5982\u53d8\u91cf\u540d\u3001\u51fd\u6570\u540d\u3001\u7c7b\u578b\u540d\u7b49\uff09\u53ea\u80fd\u5728\u672c\u5305\u5185\u8bbf\u95ee\uff0c\u662f\u4e0d\u80fd\u8de8\u5305\u8bbf\u95ee\u7684<\/mark><\/strong><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ec4141\" class=\"has-inline-color\">\u3002<\/mark><\/strong><\/p>\n\n\n\n<p><strong>\u4e5f\u5c31\u662f\uff1atype RedisConfig struct \u548c type Conf struct \u7684\u6210\u5458\u53d8\u91cf\u9700\u8981\u5927\u5199\u5f00\u5934\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>go\u5b9e\u73b0yaml\u914d\u7f6e\u6587\u4ef6\u7684\u89e3\u6790\u3002\u9700\u8981\u5efa\u7acb3\u4e2a\u6587\u4ef6\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\u3002go\u6a21\u5757\u6587\u4ef6 go.mod\uff0c\u7528\u4e8e\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u7248\u672c\u4fe1\u606f\u3002<\/p>\n","protected":false},"author":1,"featured_media":727,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[286,154],"tags":[292,287,290,293,289,291],"class_list":["post-729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-154","tag-go-mod-tidy","tag-golang","tag-module","tag-package","tag-viper","tag-291"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=729"}],"version-history":[{"count":12,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":1536,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions\/1536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/727"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}