我在使用Go的Struct中遇到问题。

示例代码:

package main
import (
    "fmt"
)
type KeyVal struct {
    Key   interface{}
    Value interface{}
}
type KeyVals []KeyVal
func (kvs *KeyVals) AddOld(key interface{}, val interface{}) {
    kv := KeyVal{key, val,typ}
    *kvs = append(*kvs, kv)
}
func (kvs *KeyVals) Add(key interface{}, val interface{}){
    var flag,id = kvs.Exist(key)
    if flag == true {
        //kv := KeyVal{key,"Updated!"}
        //*kvs=append(*kvs,kv)
        //fmt.Println(*kvs[0].Key)
        //update value of them
        kv := KeyVal{key,val}
        kvs[id]=kv
        //*kvs[id]=kv
        //fmt.Println("old set with id =",id,",","value =",kvs)
    }else{
        kv := KeyVal{key, val}
        *kvs = append(*kvs, kv)
    }
}
func (kvs *KeyVals) Search(skey interface{}) (bool,interface{},interface{}) {
    for n, kv := range *kvs {
        key := kv.Key
        val := kv.Value
        if(key == skey){
            return true,n,val
        }
    }
    return false,nil,nil
}
func (kvs *KeyVals) Exist(skey interface{})(bool,int) {
    for n, kv := range *kvs {
        key := kv.Key
        if(key == skey){
            return true,n
        }
    }
    return false,-1
}
func main() {
    var kvs keyval.KeyVals

    kvs.Add("key1","value1")
    kvs.Add("key2","value2")
    kvs.Add("key3","value3")
    kvs.Add("key4",5)
    kvs.Add("key5",5.2)
    kvs.Add("key5","new....")//this should update value of this key. but not work!


var flag,id,value = kvs.Search("key5")
fmt.Println(flag,id,value)


    for _, kv := range kvs {
        key := kv.Key
        val := kv.Value

        fmt.Println("", key," ==> ", val)
    }
}

我的问题:

函数Add()中的
  • kvs[id]=kv有错误!如何解决?
  • Exist()
  • 速度好吗?不能变得更好?
  • 我尝试在代码中添加索引名称:Search()更改为:意思是:type KeyVals []KeyValtype KeyVals [interface{}]KeyVal ...但是出错!
  • 最佳答案

    1。在函数Add()中,kvs [id] = kv有错误!如何解决?
    kvs是* KeyVals类型,它不支持索引
    您可以使用

    (*kvs)[id] = kv
    

    2。 Exist()和Search()的速度好吗?不能变得更好?

    您可以使用哈希图而不是数组来实现更快的搜索/存在功能。您可以将searchexist组合为一个功能。

    3。我尝试在代码中添加索引名称:将KeyVals [] KeyVal更改为:意思是:键入KeyVals [interface {}] KeyVal或键入KeyVals [string] KeyVal ...但出错!

    类型声明是错误的,更接近的类型是map[interface{}]interface{}map [string]interface{}
    这是有效的更新代码,我使用了Hashmap而不是array来跟踪顺序

    代码
    package main
    
    import (
        "fmt"
    )
    
    type KeyVals struct {
        keyVals     map[string]interface{}
        KeysInOrder []string
    }
    
    func (kvs *KeyVals) Add(key string, val interface{}) {
        if kvs.keyVals == nil {
            kvs.keyVals = make(map[string]interface{})
        }
        if _, ok := kvs.keyVals[key]; ok {
            kvs.keyVals[key] = val
            return
        }
        kvs.keyVals[key] = val
        kvs.KeysInOrder = append(kvs.KeysInOrder, key)
        return
    }
    
    func (kvs KeyVals) Search(key string) (interface{}, bool) {
        val, found := kvs.keyVals[key]
        return val, found
    }
    
    func main() {
        var kvs KeyVals
        kvs.Add("key1", "value1")
        kvs.Add("key2", "value2")
        kvs.Add("key3", "value3")
        kvs.Add("key4", 5)
        kvs.Add("key5", 5.2)
        kvs.Add("key5", "new....") //this should update value of this key. but not work!
    
        value, ok := kvs.Search("key5")
        fmt.Println(value, ok)
    
        for _, key := range kvs.KeysInOrder {
            value, _ = kvs.Search(key)
            fmt.Println(key, " ==> ", value)
        }
    }
    

    这是播放链接:link

    关于dictionary - 如何使用Go修复Struct中的Issue?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42039921/

    10-11 16:14