dsajkdadsa14222 2017-02-04 11:39
浏览 22
已采纳

如何使用Go修复Struct中的Issue?

I have Problem in Struct using Go.

example code:

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)
    }
}

Problem of me:

  1. in function Add() , kvs[id]=kv have error! how fix?

  2. speed of Exist() and Search() is good? can not make better?

  3. I try add index name to code : type KeyVals []KeyVal change to : mean : type KeyVals [interface{}]KeyVal or type KeyVals [string]KeyVal ... but error!

  • 写回答

1条回答 默认 最新

  • dougu3591 2017-02-04 12:09
    关注

    1 . in function Add() , kvs[id]=kv have error! how fix?

    kvs is of type *KeyVals it does not support indexing You may use

    (*kvs)[id] = kv 
    

    2 . speed of Exist() and Search() is good? can not make better?

    You may use a hashmap instead of array for faster search/exist functions.You can combine search and exist to one function.

    3 . i try add index name to code : type KeyVals []KeyVal change to : mean : type KeyVals [interface{}]KeyVal or type KeyVals [string]KeyVal ... but error!

    The type declarations are wrong.Types closer to those are map[interface{}]interface{} or map [string]interface{}

    Here is the updated code that works, I have used Hashmap instead of array and an array to keep track of the order

    Code

    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)
        }
    }
    

    Here is the play link : link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)