英文:
3 variable map in Go
问题
我正在尝试在Go语言中创建一个包含3个变量的映射,以便你可以执行类似以下的操作:
var postlist = make(map[int]map[int]bool)
postlist[postid][userid] = true
if postlist[postid][userid] == true {
// 做一些操作
}
我尝试使用自定义结构体来创建映射,类似于:
var postlist = make(map[int]cusmap)
type cusmap struct {
userid int
seen bool
}
但是我不知道如何同时检查userid和seen的条件。
英文:
I am trying to make a 3 variable map in go so that you can do something like.
var postlist = make(map[int][int]bool)
postlist[postid][userid] = true
if postid[postid][userid] = true {
//do something
}
I have tried to make my own using a struct like
var postlist = make(map[int]cusmap)
type cusmap struct {
userid int
seen bool
}
but then I don't know how to check to check both the userid and seen bool condition.
答案1
得分: 5
我不确定你想要做什么,但是一个映射(map)只能是键/值对。你不能有键/键/值对。为了实现这个,你需要将值设置为一个映射,所以你可以这样做:
package main
func main() {
var postlist = make(map[int]map[int]bool)
postid, userid := 0, 0
postlist[postid] = make(map[int]bool)
postlist[postid][userid] = true
if postlist[postid][userid] == true {
println("ok")
} else {
println("ko")
}
}
以上是一个示例代码,它创建了一个嵌套的映射,其中键是postid
,值是一个映射,该映射的键是userid
,值是一个布尔值。根据给定的postid
和userid
,你可以检查映射中的值是否为true
。
英文:
I am not sure what you are trying to do, but a map is only a key/value. You can't have key/key/value. In order to do this, you need the value to be a map, so you would do:
http://play.golang.org/p/dOAXNAI4CO
package main
func main() {
var postlist = make(map[int]map[int]bool)
postid, userid := 0, 0
postlist[postid] = make(map[int]bool)
postlist[postid][userid] = true
if postlist[postid][userid] == true {
println("ok")
} else {
println("ko")
}
}
答案2
得分: 1
如果你想实现一组int
对,你可以使用一个结构体作为映射的键:
type postData struct {
userID int
postID int
}
然后创建一个以postData
为键,bool
为值的映射:
postSet := map[postData]bool{
postData{1234, 7284}: true,
postData{7777, 1212}: true,
}
我们可以利用这样一个事实,即如果我们给映射一个不存在的索引,它将返回一个零值。
对于bool
类型,零值是false
。
if postSet[postData{7777, 1212}] {
fmt.Println("找到了帖子。")
} else {
fmt.Println("没有这样的帖子!")
}
这里有一个完整的工作示例:http://play.golang.org/p/VJw9Vm8gHA
英文:
If you want to implement a set of int
pairs, you can use a structure as the map key:
type postData struct {
userID int
postID int
}
Then make a map with postData
keys and bool
values:
postSet := map[postData]bool{
postData{1234, 7284}: true,
postData{7777, 1212}: true}
We can exploit the fact that if we give a non-existent index to the map, it will just return a zero-value.
For bool
, the zero-value is false
.
if postSet[postData{7777, 1212}] {
fmt.Println("post found.")
} else {
fmt.Println("no such post!")
}
Here's a full working example: http://play.golang.org/p/VJw9Vm8gHA
答案3
得分: 0
@creack的方法之外,还有一种方法是在地图本身上定义函数,这样你就不必每次手动检查是否要设置/取消某些东西:
func main() {
cm := CustMap{}
pid, uid := 0, 0
cm.Set(pid, uid)
fmt.Println(cm.Exists(pid, uid))
fmt.Println(cm.Exists(pid, 10))
fmt.Println(cm.Exists(10, 10))
}
type CustMap map[int]map[int]struct{} //struct使得地图在值方面使用0字节,而不是bools的1字节,当地图增长时很方便
func (cm CustMap) Set(pid, uid int) {
if _, ok := cm[pid]; !ok {
cm[pid] = make(map[int]struct{})
}
cm[pid][uid] = struct{}{}
}
func (cm CustMap) Exists(pid, uid int) (ok bool) {
if u := cm[pid]; u != nil {
_, ok = u[uid]
}
return
}
英文:
An alternative to @creack's approach is to define funcs on the map itself that way you don't have to manually check every time you want to set / unset something:
func main() {
cm := CustMap{}
pid, uid := 0, 0
cm.Set(pid, uid)
fmt.Println(cm.Exists(pid, uid))
fmt.Println(cm.Exists(pid, 10))
fmt.Println(cm.Exists(10, 10))
}
type CustMap map[int]map[int]struct{} //struct makes the map use 0 bytes for values instead of 1 bytes for bools, handy as the map grows
func (cm CustMap) Set(pid, uid int) {
if _, ok := cm[pid]; !ok {
cm[pid] = make(map[int]struct{})
}
cm[pid][uid] = struct{}{}
}
func (cm CustMap) Exists(pid, uid int) (ok bool) {
if u := cm[pid]; u != nil {
_, ok = u[uid]
}
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论