douqianni4080 2014-07-28 19:11
浏览 39
已采纳

Go中的3个变量映射

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.

  • 写回答

3条回答 默认 最新

  • douyan8070 2014-07-28 19:22
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用