{"id":207499,"date":"2025-07-08T08:48:16","date_gmt":"2025-07-08T00:48:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207499\/"},"modified":"2025-07-08T08:48:16","modified_gmt":"2025-07-08T00:48:16","slug":"%e5%9c%a8-go-%e4%b8%ad%e4%bd%bf%e7%94%a8-struct-%e4%bd%9c%e4%b8%ba%e5%8c%85%e8%a3%85%e5%99%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207499\/","title":{"rendered":"\u5728 go \u4e2d\u4f7f\u7528 struct \u4f5c\u4e3a\u5305\u88c5\u5668"},"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>\u5728 go \u4e2d\u4f7f\u7528 struct \u4f5c\u4e3a\u5305\u88c5\u5668<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 08:51:26<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 go \u4e2d\u4f7f\u7528 struct \u4f5c\u4e3a\u5305\u88c5\u5668\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u5c06 <code>redis.client<\/code> \u5305\u88c5\u5230\u6211\u7684 <code>struct<\/code> \u4e2d\uff1f\u6211\u6709\u8be5\u4ee3\u7801\u5e76\u7ed9\u4e86\u6211\u4e00\u4e2a\u9519\u8bef<\/p>\n<pre>package main\n\nimport (\n    \"github.com\/go-redis\/redis\"\n)\n\nvar cache *rediscache\n\n\/\/ rediscache struct\ntype rediscache struct {\n    *redis.client\n}\n\nfunc initcache() *rediscache {\n    if cache == nil {\n        cache = redis.newclient(&amp;redis.options{\n           addr:     \"localhost:6379\",\n            password: \"\",\n            db:       0,\n        })\n    }\n\n    return cache\n}<\/pre>\n<pre>cannot use redis.NewClient(&amp;redis.Options literal) (type *redis.Client) as type *RedisCache in assignment<\/pre>\n<p>\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8f6c\u6362\u8be5\u5c5e\u6027\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>redis.newclient()<\/code> \u8fd4\u56de <code>*redis.client<\/code> \u7c7b\u578b\u7684\u503c\u3002\u4f7f\u7528 \u521b\u5efa <code>rediscache<\/code> \u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u5206\u914d\u7ed9 <code>cache<\/code>\uff1a<\/p>\n<pre>func initCache() *RedisCache {\n    if cache == nil {\n        client := redis.NewClient(&amp;redis.Options{\n            Addr:     \"localhost:6379\",\n            Password: \"\",\n            DB:       0,\n        })\n        cache = &amp;RedisCache{Client: client}\n    }\n\n    return cache\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728 go \u4e2d\u4f7f\u7528 struct \u4f5c\u4e3a\u5305\u88c5\u5668\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207499","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207499","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=207499"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207499\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}