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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?