file2cable
file2cable

Reputation: 584

Golang append to a slice of type

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?

Upvotes: 0

Views: 1411

Answers (1)

kostya
kostya

Reputation: 9559

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)

Upvotes: 2

Related Questions