英文:
Golang Map struct in another one
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是新手在使用golang,我有一个包含结构体的[string]map。
目前一切都正常。
但是我想要有一个[string]map,其中可以同时访问另一个[string]map,而该map本身也有结构体。
这是我尝试工作的代码:
type myStruct struct {
atrib1 string
atrib2 string
}
var apiRequest map[string]map[string]myStruct
我想要像这样访问:
func main() {
apiRequest = make(map[string]map[string]myStruct)
apiRequest["Key"]["MyFirstOption"].atrib1 = "first Value first op"
apiRequest["Key"]["MyFirstOption"].atrib2 = "second Value first op"
apiRequest["Key"]["MysecondtOption"].atrib1 = "first Value second op"
}
希望对你有帮助!
英文:
I'm new working on golang, In this case I have a map [string] which has a struct.
At this point everything is works.
But I would like to have a map[string] in which I can access at the same time to another map[string] which has it self struct.
This is the code in which I'm trying to work.
type myStruct struct{
atrib1 string
atrib2 string
}
var apiRequest map[string] map[string]myStruct
I would like acces to something like this:
func main() {
apiRequest = make(map[string] map[string]myStruct)
apiKeyTypeRequest["Key"]["MyFirstOption"].atrib1 = "first Value first op"
apiKeyTypeRequest["Key"]["MyFirstOption"].atrib2 = "second Value first op"
apiKeyTypeRequest["Key"]["MysecondtOption"].atrib1 = "first Value second op"
}
答案1
得分: 10
使用一个单独的map[mapKey]
,其中mapKey
是一个结构体,是使用嵌套map的一种替代方法:
type mapKey struct {
Key string
Option string
}
优点是在搜索`myStruct`时只需要进行一次查找,并且只需要创建一个map。
缺点是,如果需要获取该选项的`map[string]myStruct`映射,由于它不存在,无法实现。此外,无法检查某个键是否存在,因为键和选项是成对存在的。
**工作示例:**
```go
package main
import "fmt"
type myStruct struct {
atrib1 string
atrib2 string
}
type mapKey struct {
Key string
Option string
}
func main() {
apiKeyTypeRequest := make(map[mapKey]myStruct)
apiKeyTypeRequest[mapKey{"Key", "MyFirstOption"}] = myStruct{"first Value first op", "second Value first op"}
apiKeyTypeRequest[mapKey{"Key", "MysecondtOption"}] = myStruct{atrib1: "first Value second op"}
fmt.Printf("%+v\n", apiKeyTypeRequest)
}
Playground: http://play.golang.org/p/tGd7ja7QI2
英文:
An alternative to using a map inside a map, is to have a single map[mapKey]
where mapKey
is a struct:
type mapKey struct {
Key string
Option string
}
The benefits is that you only have to do a single lookup when searching for a myStruct
, and you only have to create a single map.
The downside is in case you need to be able to get that options map[string]myStruct
map, since it does not exist. Also, you cannot check if a certain key exists or not, because keys and options exists in pairs.
Working example:
package main
import "fmt"
type myStruct struct {
atrib1 string
atrib2 string
}
type mapKey struct {
Key string
Option string
}
func main() {
apiKeyTypeRequest := make(map[mapKey]myStruct)
apiKeyTypeRequest[mapKey{"Key", "MyFirstOption"}] = myStruct{"first Value first op", "second Value first op"}
apiKeyTypeRequest[mapKey{"Key", "MysecondtOption"}] = myStruct{atrib1: "first Value second op"}
fmt.Printf("%+v\n", apiKeyTypeRequest)
}
Playground: http://play.golang.org/p/tGd7ja7QI2
答案2
得分: 6
为了扩展之前的回答,每个地图都必须声明和实例化(以及地图末尾的结构体),这意味着您需要实例化“外部”地图:
mapOfMaps := make(map[string]map[string]myStruct)
以及每个键对应的“内部”地图:
mapOfMaps[key] = make(map[string]myStruct)
在这里你会遇到一个明显的问题,如何动态地检查mapOfMaps[key]
是否已经实例化?你可以使用以下语法来实现:
if _, ok := mapOfMaps[key]; !ok {
mapOfMaps[key] = make(map[string]myStruct)
}
这个语法用于检查mapOfMaps
是否已经为该键实例化了一个值,如果没有,则进行实例化。类似地,您可以使用相同的语法来实例化您正在使用的结构体,通过检查mapOfMaps[key][key2]
是否已经实例化:
if _, ok := mapOfMaps[key][key2]; !ok {
mapOfMaps[key][key2] = new(myStruct)
}
现在,您可以通过调用两个键和结构体属性来访问结构体:
fmt.Println(mapOfMaps[key][key2].atrib1)
英文:
To expand upon previous answers, each map must be declared and instantiated (as well as the struct at the end of the map), that means you'll need to instantiate the "outer" map
mapOfMaps := make(map[string]map[string]myStruct)
as well as the "inner" map(s) for each key you have.
mapOfMaps[key] = make(map[string]myStruct)
The obvious issue you run into here is how do you dynamically check to see if the mapOfMaps[key]
has already been instantiated? You do this using the following syntax:
if _, ok := mapOfMaps[key]; !ok {
mapOfMaps[key] = make(map[string]myStruct)
}
This syntax checks to see if mapOfMaps
already has an instantiated value for that key, and if not instantiates it. Similarly, you can use the same syntax to instantiate the structs you are using by checking to see of mapOfMaps[key][key2]
has been instantiated yet.
if _, ok := mapOfMaps[key][key2]; !ok {
mapOfMaps[key][key2] = new(myStruct)
}
Now you can access the struct through the call to 2 keys and an attribute of the struct
fmt.Println(mapOfMaps[key][key2].atrib1)
答案3
得分: 2
如@FUZxxl所说,你需要为每个外部映射初始化子映射,但是有一种简写语法可以实现:
type myStruct struct {
atrib1 string
atrib2 string
}
func main() {
var myMap = map[string]map[string]myStruct{
"foo": {
"bar": {atrib1: "a", atrib2: "b"},
"baz": {"c", "d"}, //或者按顺序...
},
"bar": {
"gaz": {"e", "f"},
"faz": {"g", "h"},
},
}
fmt.Println(myMap["foo"]["bar"].atrib1)
fmt.Println(myMap["bar"]["gaz"].atrib2)
}
英文:
As @FUZxxl has said, you need to initialize the sub-map for each outer map, but there is a shorthand syntax for it:
type myStruct struct {
atrib1 string
atrib2 string
}
func main() {
var myMap = map[string]map[string]myStruct{
"foo": {
"bar": {attrib1: "a", attrib2: "b"},
"baz": {"c", "d"}, //or by order...
},
"bar": {
"gaz": {"e", "f"},
"faz": {"g", "h"},
},
}
fmt.Println(myMap["foo"]["bar"].atrib1)
fmt.Println(myMap["bar"]["gaz"].atrib2)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论