{"id":207059,"date":"2025-07-08T08:56:41","date_gmt":"2025-07-08T00:56:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207059\/"},"modified":"2025-07-08T08:56:41","modified_gmt":"2025-07-08T00:56:41","slug":"%e5%a6%82%e4%bd%95%e9%87%8d%e7%94%a8%e7%ac%ac%e4%b8%89%e6%96%b9%e5%8c%85%e4%b8%ad%e7%9a%84%e7%bb%93%e6%9e%84%ef%bc%8c%e5%90%8c%e6%97%b6%e6%9b%b4%e6%94%b9%e5%8d%95%e4%b8%aa%e5%ad%97%e6%ae%b5%e7%9a%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207059\/","title":{"rendered":"\u5982\u4f55\u91cd\u7528\u7b2c\u4e09\u65b9\u5305\u4e2d\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u66f4\u6539\u5355\u4e2a\u5b57\u6bb5\u7684\u7f16\u7ec4\u884c\u4e3a\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\u91cd\u7528\u7b2c\u4e09\u65b9\u5305\u4e2d\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u66f4\u6539\u5355\u4e2a\u5b57\u6bb5\u7684\u7f16\u7ec4\u884c\u4e3a\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 19:24:34<\/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\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u91cd\u7528\u7b2c\u4e09\u65b9\u5305\u4e2d\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u66f4\u6539\u5355\u4e2a\u5b57\u6bb5\u7684\u7f16\u7ec4\u884c\u4e3a\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u60f3\u5c06\u4e00\u4e2a\u7ed3\u6784\u7f16\u7ec4\u5230 YAML \u4e2d\uff0c\u5e76\u4e14\u8be5\u7ed3\u6784\u5df2\u7ecf\u5b9a\u4e49\u4e86\u5176\u6240\u6709 YAML \u6807\u8bb0\uff0c\u4f46\u6709\u4e00\u4e2a\u6211\u60f3\u8981\u66f4\u6539\u7684\u6807\u8bb0\u9664\u5916\u3002\u5982\u4f55\u5728\u4e0d\u66f4\u6539\u7ed3\u6784\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u66f4\u6539\u6b64\u5355\u4e2a\u5b57\u6bb5\u7684\u884c\u4e3a\uff1f\u5047\u8bbe\u8be5\u7ed3\u6784\u6765\u81ea\u7b2c\u4e09\u65b9\u5305\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u8981\u6f14\u793a\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u6211\u7684\u6700\u4f73\u5c1d\u8bd5\u3002\u5047\u8bbe <code>User<\/code> \u7ed3\u6784\uff08\u53ca\u5176\u5173\u8054\u7684 <code>Secret<\/code> \u7ed3\u6784\uff09\u6765\u81ea\u7b2c\u4e09\u65b9\u5305\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u5b83\u4eec\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n\n    \"gopkg.in\/yaml.v2\"\n)\n\ntype User struct {\n    Email    string  `yaml:\"email\"`\n    Password *Secret `yaml:\"password\"`\n}\n\ntype Secret struct {\n    s string\n}\n\n\/\/ MarshalYAML implements the yaml.Marshaler interface for Secret.\nfunc (sec *Secret) MarshalYAML() (interface{}, error) {\n    if sec != nil {\n        \/\/ Replace `\"&lt;secret&gt;\"` with `sec.s`, and it gets the desired\n        \/\/ behavior. But I can't change the Secret struct:\n        return \"&lt;secret&gt;\", nil\n    }\n    return nil, nil\n}\n\nfunc (sec *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {\n    var st string\n    if err := unmarshal(&amp;st); err != nil {\n        return err\n    }\n    sec.s = st\n    return nil\n}\n\n\/\/ My best attempt at the solution:\ntype SolutionAttempt struct {\n    User\n}\n\nfunc (sol *SolutionAttempt) MarshalYAML() (interface{}, error) {\n    res, err := yaml.Marshal(\n        struct {\n            \/\/ I don't like having to repeat all these fields from User:\n            Email    string `yaml:\"email\"`\n            Password string `yaml:\"password\"`\n        }{\n            Email:    sol.User.Email,\n            Password: sol.User.Password.s,\n        },\n    )\n    if err != nil {\n        return nil, err\n    }\n    return string(res), nil\n}\n\nfunc main() {\n    user := &amp;User{}\n    var data = `\n  email: [email&nbsp;protected]\n  password: asdf\n`\n    err := yaml.Unmarshal([]byte(data), user)\n    if err != nil {\n        fmt.Printf(\"errors! %s\", err)\n        return\n    }\n\n    buf, err := yaml.Marshal(user)\n    if err != nil {\n        fmt.Printf(\"errors! %s\", err)\n        return\n    }\n\n    \/\/ Without touching User or Secret, how can I unmarshall an\n    \/\/ instance of User that renders the secret?\n    fmt.Printf(\"marshalled output:\\n%s\\n\", buf)\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ attempted solution:\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    sol := &amp;SolutionAttempt{}\n    var data2 = `\nuser:\n    email: [email&nbsp;protected]\n    password: asdf\n`\n    err = yaml.Unmarshal([]byte(data2), sol)\n    if err != nil {\n        fmt.Printf(\"errors! %s\", err)\n        return\n    }\n\n    buf, err = yaml.Marshal(sol)\n    if err != nil {\n        fmt.Printf(\"errors! %s\", err)\n        return\n    }\n    fmt.Printf(\"attempted solution marshalled output:\\n%s\\n\", buf)\n}<\/pre>\n<p>\u8fd9\u662f\u4e0a\u8ff0\u4ee3\u7801\u7684 Go Playground \u94fe\u63a5\uff1ahttps:\/\/go.dev\/play\/p\/ojiPv4ylCEq<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u6839\u672c\u4e0d\u53ef\u80fd\u3002<\/p>\n<p>\u4f60\u7684\u201c\u6700\u4f73\u5c1d\u8bd5\u201d\u5c31\u662f\u6b63\u786e\u7684\u9053\u8def\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u91cd\u7528\u7b2c\u4e09\u65b9\u5305\u4e2d\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u66f4\u6539\u5355\u4e2a\u5b57\u6bb5\u7684\u7f16\u7ec4\u884c\u4e3a\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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-207059","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207059","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=207059"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207059\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}