简体   繁体   English

如何使用 Go 修复 Struct 中的问题?

[英]How fix Issue in Struct using Go?

I have Problem in Struct using Go.我在使用 Go 的 Struct 中有问题。

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!在函数 Add() 中, kvs[id]=kv有错误! how fix?怎么修?

  2. speed of Exist() and Search() is good? Exist()Search()速度好吗? 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!我尝试将索引名称添加到代码中: type KeyVals []KeyVal更改为:意思是: type KeyVals [interface{}]KeyValtype KeyVals [string]KeyVal ...但错误!

1 . 1 . in function Add() , kvs[id]=kv have error!在函数 Add() 中, kvs[id]=kv 有错误! how fix?怎么修?

kvs is of type *KeyVals it does not support indexing You may use kvs是 *KeyVals 类型,它不支持索引您可以使用

(*kvs)[id] = kv 

2 . 2 . speed of Exist() and Search() is good? Exist() 和 Search() 的速度好吗? 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.您可以使用哈希图而不是数组来实现更快的搜索/存在函数。您可以将searchexist结合到一个函数中。

3 . 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!我尝试将索引名称添加到代码:键入 KeyVals []KeyVal 更改为:意思是:键入 KeyVals [interface{}]KeyVal 或键入 KeyVals [string]KeyVal ...但错误!

The type declarations are wrong.Types closer to those are map[interface{}]interface{} or map [string]interface{}类型声明是错误的。更接近这些的类型是map[interface{}]interface{}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这是有效的更新代码,我使用 Hashmap 而不是数组和数组来跟踪订单

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这是播放链接:链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM