함수가 if else 문에서 작동하지 않음 [중복]

Jan 15 2021

Go를 튜토리얼로 사용하여 기본 캐시 시스템을 구축하려고합니다.

어떤 이유로 내가 이것을 실행할 때 if else 문은 오류로 이어집니다.

package datasource

import (
    "fmt"
    "github.com/patrickmn/go-cache"
    "time"
)

type DataSource interface {
    Value(key string) (interface{}, error)

}

// DataSourceStr type, implements the DataSource interface
type 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 heheh Jan 15 2021 at 14:39

go 조건문은 다음 구문을 따릅니다.

   if something{
      ......

   } else if something{
      ......

   } else{
      ......
   }

자세한 답변