如何在不覆盖现有数据的情况下向变量添加键值对?

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

How do I add key-value to variable without overriding existing data?

问题

我正在尝试创建一个简单的缓存,只使用一个变量,但我不知道如何在不完全覆盖它的情况下添加到它。

这是一个代码片段:

package main

var store map[string][]byte

type Routes struct{}

func NewRoutes() *Routes {
    return &Routes{}
}

func (c *Routes) SetRoutes(key string, routes []byte) error {
    store[key] = routes

    return nil
}

func main() {
    routes := NewRoutes()

    routes.SetRoutes("key", []byte("routes"))
}

这将导致panic: assignment to entry in nil map错误。我可以使用make()函数创建store切片,但我不想这样做,因为我认为这样会丢失切片中已有的内容,使其变得无用。

在Go语言中,你可以通过使用make()函数来初始化store变量,而不会丢失已有的内容。你可以将以下代码添加到main()函数中:

store = make(map[string][]byte)

这将创建一个空的store映射,而不会覆盖已有的内容。这样,你就可以在不完全覆盖的情况下向缓存中添加新的键值对了。

你可以在这里查看修改后的代码:Go Playground

英文:

I am trying to create a simple cache just using a variable and I don't know how to add to it without completely overriding it each time.

Here is a snippet:

package main

var store map[string][]byte

type Routes struct{}

func NewRoutes() *Routes {
    return &Routes{}
}

func (c *Routes) SetRoutes(key string, routes []byte) error {
    store[key] = routes

    return nil
}

func main() {
    routes := NewRoutes()

    routes.SetRoutes("key", []byte("routes"))
}

This will result in panic: assignment to entry in nil map. I can use make() to create the store slice - but I don't want to do that as I believe I would lose whatever was already in the slice rendering it useless.

How can I do this in go?

https://go.dev/play/p/NbqSbSTx5ER

答案1

得分: 4

你正在创建一个全局变量存储,但是你可能想要将其封装在你的Routes结构体中:

package main

type Routes struct{
    store map[string][]byte
}

func NewRoutes() *Routes {
    return &Routes{
        store: make(map[string][]byte),
    }
}

func (c *Routes) SetRoutes(key string, routes []byte) error {
    c.store[key] = routes

    return nil
}

func main() {
    routes := NewRoutes()

    routes.SetRoutes("key", []byte("routes"))
}

参考:https://go.dev/play/p/3M4kAfya6KE。这样可以确保map的作用域限定在你的Routes结构体中,并且只初始化一次。

英文:

You are creating a global variable store, while most likely you want to encapsulate it in your Routes struct:

package main

type Routes struct{
    store map[string][]byte
}

func NewRoutes() *Routes {
    return &Routes{
        store: make(map[string][]byte),
    }
}

func (c *Routes) SetRoutes(key string, routes []byte) error {
    c.store[key] = routes

    return nil
}

func main() {
    routes := NewRoutes()

    routes.SetRoutes("key", []byte("routes"))
}

See: https://go.dev/play/p/3M4kAfya6KE. This ensures the map is scoped to your Routes struct and only initialised once.

答案2

得分: 3

你可以检查它是否为nil,如果是的话就进行make操作:

func (c *Routes) SetRoutes(key string, routes []byte) error {
    if store == nil {
        store = make(map[string][]byte)
    }
    store[key] = routes
    return nil
}

或者,你可以在main函数中直接进行make操作:

func main() {
    store = make(map[string][]byte)
    routes := NewRoutes()
    routes.SetRoutes("key", []byte("routes"))
}
英文:

You can check if it is nil and make if it is:

func (c *Routes) SetRoutes(key string, routes []byte) error {
    if store == nil {
        store = make(map[string][]byte)
    }
    store[key] = routes
    return nil
}

Alternatively, just make it in the main func:

func main() {
    store = make(map[string][]byte)
    routes := NewRoutes()
    routes.SetRoutes("key", []byte("routes"))
}

huangapple
  • 本文由 发表于 2022年3月17日 22:16:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/71513851.html
匿名

发表评论

匿名网友

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

确定