英文:
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?
答案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"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论