解决 Golang 中多泛型函数调用的问题
来源:stackoverflow
时间:2024-02-13 14:57:16 474浏览 收藏
今天golang学习网给大家带来了《解决 Golang 中多泛型函数调用的问题》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~
问题内容
我想从远程获取配置并缓存结果。该 api 有两个数据结构,但处理方式相同。所以我在流程函数中使用泛型,当缓存数据与远程配置不同时,更新缓存数据,但我在函数 savekeystore 中收到错误,old != new (类型集中的不可比较类型)
,这是我的代码 p>
package main import ( "encoding/json" "errors" "fmt" "net/http" "net/url" "sync" ) var m sync.Map type AccountResponse[Payload AppleKeyStore | GoogleKeyStore] struct { Data struct { Options Payload `json:"options"` } `json:"data"` Code int `json:"code"` } type AppleKeyStore struct { KeyID string `json:"kid"` SharedPasswords []struct { BundleID string `json:"bundle_id"` Password string `json:"password"` } `json:"shared_passwords"` } type GoogleKeyStore struct { AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"` AuthUri string `json:"auth_uri"` Bucket string `json:"bucket"` } func getCachedKeyStore[Payload AppleKeyStore | GoogleKeyStore](appID string) (*Payload, error) { value, ok := m.Load(appID) if ok { t := value.(Payload) return &t, nil } else { return nil, errors.New("cache keystore not exist") } } func saveKeyStore[T AppleKeyStore | GoogleKeyStore](appID string, old, new T) { if old != new { m.Store(appID, new) } } func fetchRemoteAccount[Payload AppleKeyStore | GoogleKeyStore](accountID string) (*AccountResponse[Payload], error) { urlStr := "" client := &http.Client{} params := url.Values{} params.Add("id", accountID) req, err := http.NewRequest(http.MethodGet, urlStr, nil) if err != nil { return nil, err } req.URL.RawQuery = params.Encode() req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", "token")) resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("fetch secret api, response status code: %d", resp.StatusCode) } var appleAccountResponse AccountResponse[Payload] err = json.NewDecoder(resp.Body).Decode(&appleAccountResponse) if err != nil { return nil, err } return &appleAccountResponse, nil } func getKeyStoreFromRemoteOrCache[Payload GoogleKeyStore | AppleKeyStore](accountID, appID string) (*Payload, error) { remoteAccountRespone, fetchErr := fetchRemoteAccount[Payload](accountID) cachedKeyStore, getCacheErr := getCachedKeyStore[Payload](appID) if fetchErr != nil { if getCacheErr != nil { return nil, errors.New("fetch account failed and get cache apple account failed") } else { return cachedKeyStore, nil } } else { saveKeyStore(appID, *cachedKeyStore, remoteAccountRespone.Data.Options) return &remoteAccountRespone.Data.Options, nil } } func main() { getKeyStoreFromRemoteOrCache[GoogleKeyStore]("", "") }
我尝试定义泛型接口,但似乎泛型类型无法从其他泛型类型扩展,或者golang无法将结构类型applekeystore插入到类似类型。
正确答案
定义泛型函数时,必须对其类型的所有可能值进行编译。 saveKeyStore
函数对 GoogleKeyStore
有效,但对 AppleKeyStore
无效,因为 AppleKeyStore
不可比较:它有一个切片。
您可以考虑为这两种类型编写 IsEqual
方法,并使用要求类型具有 IsEqual
方法的约束。
今天关于《解决 Golang 中多泛型函数调用的问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习