为了账号安全,请及时绑定邮箱和手机立即绑定

函数不适用于 if else 语句

函数不适用于 if else 语句

Go
宝慕林4294392 2022-08-01 16:53:45
我正在尝试使用Go作为教程构建一个基本的缓存系统。由于某种原因,当我运行这个时,if else语句会导致错误。package datasourceimport (    "fmt"    "github.com/patrickmn/go-cache"    "time")type DataSource interface {    Value(key string) (interface{}, error)}// DataSourceStr type, implements the DataSource interfacetype DataSourceStr struct {    data map[string]string}var cache = cache.New(5*time.Minute, 5*time.Minute)func (n *DataSourceStr) Value(key string) (interface{}, error) {        /*     1. Compute a cache key    2. Search cache key    3. If hit return value    4. If miss, do datasource    5. Cache and return slow thing.    */        cached, found := cache.Value(key)    if found {        return cached, nil    }    else if _, ok := n.data[key]; ok     {        //measure how often a key gets called.        cache.Set(key, n.data[key], cache.DefaultExpiration)        return n.data[key], nil    } else {        return nil, fmt.Errorf("key not found %v", ok)    }}func getFromDS(datasource DataSource, key string) (string, error) {        v, err := datasource.Value(key)        //create a map that decays based on time.    if err != nil {        return "", nil    }    return fmt.Sprint(v), nil}我做错了什么?我正在尝试输入一个密钥,然后从缓存或数据库中返回值。不知道我在语法上做错了什么!
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

go 条件语句遵循以下语法


   if something{

      ......


   } else if something{

      ......


   } else{

      ......

   }


查看完整回答
反对 回复 2022-08-01
  • 1 回答
  • 0 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号