解决 Golang 中多泛型函数调用的问题
时间:2024-02-13
浏览: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学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.29
热门专题
