Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 257c8dc

Browse files
author
Jamie Hannaford
committed
Pre-allocate the slices
1 parent 18b45ed commit 257c8dc

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

openstack/db/v1/databases/requests.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ type BatchCreateOpts []CreateOpts
6161

6262
// ToDBCreateMap renders a JSON map for creating DBs.
6363
func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) {
64-
var dbs []map[string]string
65-
for _, db := range opts {
64+
dbs := make([]map[string]string, len(opts))
65+
for i, db := range opts {
6666
dbMap, err := db.ToMap()
6767
if err != nil {
6868
return nil, err
6969
}
70-
dbs = append(dbs, dbMap)
70+
dbs[i] = dbMap
7171
}
7272
return map[string]interface{}{"databases": dbs}, nil
7373
}

openstack/db/v1/users/requests.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ func (opts CreateOpts) ToMap() (map[string]interface{}, error) {
6060
user["host"] = opts.Host
6161
}
6262

63-
var dbs []map[string]string
64-
for _, db := range opts.Databases {
65-
dbs = append(dbs, map[string]string{"name": db.Name})
63+
dbs := make([]map[string]string, len(opts.Databases))
64+
for i, db := range opts.Databases {
65+
dbs[i] = map[string]string{"name": db.Name}
6666
}
67+
6768
if len(dbs) > 0 {
6869
user["databases"] = dbs
6970
}
@@ -76,13 +77,13 @@ type BatchCreateOpts []CreateOpts
7677

7778
// ToUserCreateMap will generate a JSON map.
7879
func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
79-
var users []map[string]interface{}
80-
for _, opt := range opts {
80+
users := make([]map[string]interface{}, len(opts))
81+
for i, opt := range opts {
8182
user, err := opt.ToMap()
8283
if err != nil {
8384
return nil, err
8485
}
85-
users = append(users, user)
86+
users[i] = user
8687
}
8788
return map[string]interface{}{"users": users}, nil
8889
}

0 commit comments

Comments
 (0)