如何在Go中修复使用结构体的问题?

huangapple go评论70阅读模式
英文:

How fix Issue in Struct using Go?

问题

我在使用Go语言的结构体时遇到了问题。

以下是示例代码:

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}
    *kvs = append(*kvs, kv)
}
func (kvs *KeyVals) Add(key interface{}, val interface{}){
    var flag, id = kvs.Exist(key)
    if flag == true {
        kv := KeyVal{key, val}
        kvs[id] = kv
    } 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 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....") // 这应该更新该键的值,但没有起作用!

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

我的问题:

  1. Add()函数中,kvs[id]=kv出现错误!如何修复?

  2. Exist()Search()的速度如何?有没有更好的方法?

  3. 我尝试给代码添加索引名称:将type KeyVals []KeyVal改为type KeyVals [interface{}]KeyValtype KeyVals [string]KeyVal,但出现错误!

英文:

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

得分: 1

1. 在Add()函数中,kvs[id]=kv出现错误!如何修复?

kvs的类型是*KeyVals,它不支持索引操作。你可以使用以下方式进行修复:

(*kvs)[id] = kv

2. Exist()和Search()的速度还可以吗?有没有更好的方法?

你可以使用哈希表(hashmap)代替数组来加快搜索和存在性检查的速度。你还可以将searchexist合并为一个函数。

3. 我尝试给代码添加索引名称:将类型KeyVals []KeyVal更改为KeyVals [interface{}]KeyValKeyVals [string]KeyVal,但出现错误!

类型声明是错误的。更接近你想要的类型声明是map[interface{}]interface{}map[string]interface{}

以下是更新后的代码,我使用了哈希表(hashmap)代替数组,并使用一个数组来跟踪顺序:

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

这是Playground链接:链接

英文:

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

huangapple
  • 本文由 发表于 2017年2月4日 19:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/42039921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定