duannaiying9662 2016-02-15 05:31
浏览 62
已采纳

Golang追加到类型的切片

I am doing an ldap query, and I want to populate the result into a slice. The result looks something like

objectClass [top person organizationalPerson user]
cn [user.1]
sn [one]
description [user.1]
givenName [user]
distinguishedName [CN=user.1,OU=random,DC=example,DC=com]
...

I am trying to populate it to a map and for that, I created a type.

    type keyvalue map[string]interface{}

Now I want to create a slice of this type, so that the data would look something like this for multiple users taken

objectClass [top person organizationalPerson user]
cn [user.1]
sn [one]
description [user.1]
givenName [user]
distinguishedName [CN=user.1,OU=random,DC=example,DC=com]
...
objectClass [top person organizationalPerson user]
cn [user.2]
sn [one]
description [user.2]
givenName [user]
distinguishedName [CN=user.2,OU=random,DC=example,DC=com]
...

For that, I created a slice of the type that I created above.

userslice := make([]keyvalue, 1, 1)

How will I append each users's parameters into the slice in each iteration?

  • 写回答

1条回答 默认 最新

  • duanmu5039 2016-02-15 06:42
    关注

    Just use keyvalue instead of map[string]interface{} in your code:

    type keyvalue map[string]interface{}
    
    ....
    
    user1 := make(keyvalue)
    user1["distinguishedName"] = "[CN=user.1,OU=random,DC=example,DC=com]"
    user1["givenName"] = "user"
    
    var userslice []keyvalue
    userslice = append(userslice, user1)
    
    fmt.Printf("%#v", userslice)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行