英文:
Golang - Generate map with one to many relation
问题
我对golang还比较新手,我正在努力从现有的映射中生成一个一对多的关系映射。
这是我的脚本playground
解释:
我试图实现将第0个位置的每个元素与第1个、第2个...第n个位置的每个元素建立关系。
例如 - [0][0]=>[1][0], [0][0]=>[1][1], [0][1]=>[1][0], [0][1]=>[1][1], [0][0]=>[2][0], [0][1]=>[2][1]
我试图实现的最终输出 -
Array(
[0] => Array
(
[0] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
[1] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
)
[1] => Array
(
[0] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
[1] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
)
[2] => Array
(
[0] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
[1] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
)
[3] => Array
(
[0] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
[1] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
)
)
英文:
I am fairly new to golang and I am struggling to generate a one to many relationship map from existing map.
Here is my script playground
Explanation:-
I am trying to achieve the relation of each element of 0th position to each element of 1st,2nd,...nth position.
For example - [0][0]=>[1][0], [0][0]=>[1][1], [0][1]=>[1][0], [0][1]=>[1][1], [0][0]=>[2][0], [0][1]=>[2][1]
Final Output which I am trying to achieve -
Array(
[0] => Array
(
[0] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
[1] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
)
[1] => Array
(
[0] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
[1] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
)
[2] => Array
(
[0] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
[1] => Array
(
[room_rate_key] => 0|0
[amount] => 5307.84
)
)
[3] => Array
(
[0] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
[1] => Array
(
[room_rate_key] => 0|1
[amount] => 5246.98
)
)
)
答案1
得分: 2
使用一个包含两个值的结构体作为地图的键
使用该结构体作为键进行查找
package main
import "fmt"
type two struct {
k1 int
k2 int
}
func main() {
v := make(map[two]two)
v[two{1, 1}] = two{37, 38}
v[two{0, 0}] = two{1, 1}
fmt.Println(v)
}
英文:
use a struct with two values as the key for the map
to do a lookup use the struct as the key
package main
import "fmt"
type two struct {
k1 int
k2 int
}
func main() {
v := make(map[two]two)
v[two{1, 1}] = two{37, 38}
v[two{0, 0}] = two{1, 1}
fmt.Println(v)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论