I have an interface in go which wants to support saving and loading results in different databases and I want to support different types.
package cfgStorage
type WritableType interface {
~int | ~string | ~float64
}
type ConfigStorage[K, V WritableType] interface {
get(key K) (V, error)
set(key K, value V) (bool, error)
}
func GetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K) (V, error) {
res, err := storage.get(key)
return res, err
}
func SetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K, value V) (bool, error) {
res, err := storage.set(key, value)
return res, err
}
I implemented fileSystem storage for this interface as below:
type FileSystemStorage[K, V WritableType] struct {
}
func (f FileSystemStorage[K, V]) get(key K) (V, error) {
/// my code to load data from json file
}
func (f FileSystemStorage[K, V]) set(key K, value V) (bool, error) {
/// my code to save data as json file
}
BTW when I try to get an instance from fileSystem and SetValue it works, but for GetValue I faced a compiler error, my test code is as following:
var fileStorage cfgStorage.FileSystemStorage[string, string]
setResult, _ := cfgStorage.SetValue(fileStorage, "key", "value")
if setResult == false {
t.Error()
}
var result string
result, _ = cfgStorage.GetValue(fileStorage, "key")
The compile error is in the line where I called GetValue:
cannot infer V
If you have any idea how to solve this issue please let me know!