diff --git a/cli-plugins/manager/manager_test.go b/cli-plugins/manager/manager_test.go index 414e6899e70e..751f0936ad5c 100644 --- a/cli-plugins/manager/manager_test.go +++ b/cli-plugins/manager/manager_test.go @@ -46,7 +46,7 @@ func TestListPluginCandidates(t *testing.T) { ) defer dir.Remove() - var dirs []string + var dirs []string //nolint:prealloc for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} { dirs = append(dirs, dir.Join(d)) } diff --git a/cli/command/config/formatter.go b/cli/command/config/formatter.go index 4aebdb61ac7e..4f69f880b0ee 100644 --- a/cli/command/config/formatter.go +++ b/cli/command/config/formatter.go @@ -101,9 +101,9 @@ func (c *configContext) Labels() string { if mapLabels == nil { return "" } - var joinLabels []string + joinLabels := make([]string, 0, len(mapLabels)) for k, v := range mapLabels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/context/list.go b/cli/command/context/list.go index 9833581eed38..17c234e1d9e5 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -49,7 +49,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error { if err != nil { return err } - var contexts []*formatter.ClientContext + contexts := make([]*formatter.ClientContext, 0, len(contextMap)) for _, rawMeta := range contextMap { meta, err := command.GetDockerContext(rawMeta) if err != nil { diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index f3306617f0e7..4bf6e0b498f2 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -242,9 +242,9 @@ func (c *ContainerContext) Labels() string { return "" } - var joinLabels []string + joinLabels := make([]string, 0, len(c.c.Labels)) for k, v := range c.c.Labels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } @@ -262,7 +262,7 @@ func (c *ContainerContext) Label(name string) string { // If the trunc option is set, names can be truncated (ellipsized). func (c *ContainerContext) Mounts() string { var name string - var mounts []string + mounts := make([]string, 0, len(c.c.Mounts)) for _, m := range c.c.Mounts { if m.Name == "" { name = m.Source @@ -286,7 +286,7 @@ func (c *ContainerContext) LocalVolumes() string { } } - return fmt.Sprintf("%d", count) + return strconv.Itoa(count) } // Networks returns a comma-separated string of networks that the container is @@ -296,7 +296,7 @@ func (c *ContainerContext) Networks() string { return "" } - networks := []string{} + networks := make([]string, 0, len(c.c.NetworkSettings.Networks)) for k := range c.c.NetworkSettings.Networks { networks = append(networks, k) } @@ -313,7 +313,7 @@ func DisplayablePorts(ports []types.Port) string { last uint16 } groupMap := make(map[string]*portGroup) - var result []string + var result []string //nolint:prealloc var hostMappings []string var groupMapKeys []string sort.Slice(ports, func(i, j int) bool { @@ -328,7 +328,7 @@ func DisplayablePorts(ports []types.Port) string { hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type)) continue } - portKey = fmt.Sprintf("%s/%s", port.IP, port.Type) + portKey = port.IP + "/" + port.Type } group := groupMap[portKey] @@ -369,7 +369,7 @@ func formGroup(key string, start, last uint16) string { if ip != "" { group = fmt.Sprintf("%s:%s->%s", ip, group, group) } - return fmt.Sprintf("%s/%s", group, groupType) + return group + "/" + groupType } func comparePorts(i, j types.Port) bool { diff --git a/cli/command/formatter/displayutils.go b/cli/command/formatter/displayutils.go index 0c3b6ebbb08e..7847bb307ef8 100644 --- a/cli/command/formatter/displayutils.go +++ b/cli/command/formatter/displayutils.go @@ -41,7 +41,7 @@ func Ellipsis(s string, maxDisplayWidth int) string { } var ( - display []int + display = make([]int, 0, len(rs)) displayWidth int ) for _, r := range rs { diff --git a/cli/command/formatter/volume.go b/cli/command/formatter/volume.go index fa8ef7f23c1b..85f070793148 100644 --- a/cli/command/formatter/volume.go +++ b/cli/command/formatter/volume.go @@ -100,7 +100,7 @@ func (c *volumeContext) Labels() string { return "" } - var joinLabels []string + joinLabels := make([]string, 0, len(c.v.Labels)) for k, v := range c.v.Labels { joinLabels = append(joinLabels, k+"="+v) } diff --git a/cli/command/manifest/push.go b/cli/command/manifest/push.go index 9ab2e246b6c0..d556a9cbca93 100644 --- a/cli/command/manifest/push.go +++ b/cli/command/manifest/push.go @@ -191,9 +191,9 @@ func buildManifestDescriptor(targetRepo *registry.RepositoryInfo, imageManifest } func buildBlobRequestList(imageManifest types.ImageManifest, repoName reference.Named) ([]manifestBlob, error) { - var blobReqs []manifestBlob - - for _, blobDigest := range imageManifest.Blobs() { + blobs := imageManifest.Blobs() + blobReqs := make([]manifestBlob, 0, len(blobs)) + for _, blobDigest := range blobs { canonical, err := reference.WithDigest(repoName, blobDigest) if err != nil { return nil, err diff --git a/cli/command/network/formatter.go b/cli/command/network/formatter.go index ed8ac257a057..604095a9d422 100644 --- a/cli/command/network/formatter.go +++ b/cli/command/network/formatter.go @@ -101,9 +101,9 @@ func (c *networkContext) Labels() string { return "" } - var joinLabels []string + joinLabels := make([]string, 0, len(c.n.Labels)) for k, v := range c.n.Labels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/secret/formatter.go b/cli/command/secret/formatter.go index 9102f28856b8..a95047843121 100644 --- a/cli/command/secret/formatter.go +++ b/cli/command/secret/formatter.go @@ -108,9 +108,9 @@ func (c *secretContext) Labels() string { if mapLabels == nil { return "" } - var joinLabels []string + joinLabels := make([]string, 0, len(mapLabels)) for k, v := range mapLabels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/service/generic_resource_opts.go b/cli/command/service/generic_resource_opts.go index d406d06e6499..613c25574934 100644 --- a/cli/command/service/generic_resource_opts.go +++ b/cli/command/service/generic_resource_opts.go @@ -52,7 +52,7 @@ func ParseGenericResources(value []string) ([]swarm.GenericResource, error) { // genericResourcesFromGRPC converts a GRPC GenericResource to a GenericResource func genericResourcesFromGRPC(genericRes []*swarmapi.GenericResource) []swarm.GenericResource { - var generic []swarm.GenericResource + generic := make([]swarm.GenericResource, 0, len(genericRes)) for _, res := range genericRes { var current swarm.GenericResource @@ -95,7 +95,7 @@ func buildGenericResourceMap(genericRes []swarm.GenericResource) (map[string]swa } func buildGenericResourceList(genericRes map[string]swarm.GenericResource) []swarm.GenericResource { - var l []swarm.GenericResource + l := make([]swarm.GenericResource, 0, len(genericRes)) for _, res := range genericRes { l = append(l, res) diff --git a/cli/command/service/opts.go b/cli/command/service/opts.go index 05b3b8055dcf..d0b73a0ef786 100644 --- a/cli/command/service/opts.go +++ b/cli/command/service/opts.go @@ -375,8 +375,9 @@ func resolveNetworkID(ctx context.Context, apiClient client.NetworkAPIClient, ne } func convertNetworks(networks opts.NetworkOpt) []swarm.NetworkAttachmentConfig { - var netAttach []swarm.NetworkAttachmentConfig - for _, net := range networks.Value() { + nws := networks.Value() + netAttach := make([]swarm.NetworkAttachmentConfig, 0, len(nws)) + for _, net := range nws { netAttach = append(netAttach, swarm.NetworkAttachmentConfig{ Target: net.Target, Aliases: net.Aliases, diff --git a/cli/command/service/update.go b/cli/command/service/update.go index ff6f2fcf95f8..f6429204ef91 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -728,7 +728,7 @@ func updateUlimits(flags *pflag.FlagSet, ulimits []*units.Ulimit) []*units.Ulimi } } - var limits []*units.Ulimit + limits := make([]*units.Ulimit, 0, len(newUlimits)) for _, ulimit := range newUlimits { limits = append(limits, ulimit) } @@ -1311,7 +1311,7 @@ func updateNetworks(ctx context.Context, apiClient client.NetworkAPIClient, flag } existingNetworks := make(map[string]struct{}) - var newNetworks []swarm.NetworkAttachmentConfig + var newNetworks []swarm.NetworkAttachmentConfig //nolint:prealloc for _, network := range specNetworks { if _, exists := idsToRemove[network.Target]; exists { continue @@ -1510,7 +1510,7 @@ func capsList(caps map[string]bool) []string { if caps[opts.AllCapabilities] { return []string{opts.AllCapabilities} } - var out []string + out := make([]string, 0, len(caps)) for c := range caps { out = append(out, c) } diff --git a/cli/command/stack/loader/loader.go b/cli/command/stack/loader/loader.go index 22352f434e1b..49b5f16497ff 100644 --- a/cli/command/stack/loader/loader.go +++ b/cli/command/stack/loader/loader.go @@ -60,7 +60,7 @@ func getDictsFrom(configFiles []composetypes.ConfigFile) []map[string]interface{ } func propertyWarnings(properties map[string]string) string { - var msgs []string + msgs := make([]string, 0, len(properties)) for name, description := range properties { msgs = append(msgs, fmt.Sprintf("%s: %s", name, description)) } @@ -115,7 +115,7 @@ func buildEnvironment(env []string) (map[string]string, error) { } func loadConfigFiles(filenames []string, stdin io.Reader) ([]composetypes.ConfigFile, error) { - var configFiles []composetypes.ConfigFile + configFiles := make([]composetypes.ConfigFile, 0, len(filenames)) for _, filename := range filenames { configFile, err := loadConfigFile(filename, stdin) diff --git a/cli/command/stack/swarm/list.go b/cli/command/stack/swarm/list.go index 271cffdd0075..42fd7f414eb9 100644 --- a/cli/command/stack/swarm/list.go +++ b/cli/command/stack/swarm/list.go @@ -36,7 +36,7 @@ func GetStacks(dockerCli command.Cli) ([]*formatter.Stack, error) { ztack.Services++ } } - var stacks []*formatter.Stack + stacks := make([]*formatter.Stack, 0, len(m)) for _, stack := range m { stacks = append(stacks, stack) } diff --git a/cli/command/swarm/init.go b/cli/command/swarm/init.go index 8792e3ff505d..e19e50ce5937 100644 --- a/cli/command/swarm/init.go +++ b/cli/command/swarm/init.go @@ -66,11 +66,10 @@ func newInitCommand(dockerCli command.Cli) *cobra.Command { } func runInit(dockerCli command.Cli, flags *pflag.FlagSet, opts initOptions) error { - var defaultAddrPool []string - client := dockerCli.Client() ctx := context.Background() + defaultAddrPool := make([]string, 0, len(opts.defaultAddrPools)) for _, p := range opts.defaultAddrPools { defaultAddrPool = append(defaultAddrPool, p.String()) } diff --git a/cli/command/trust/common_test.go b/cli/command/trust/common_test.go index 11a0bc86c31b..13f24f50222b 100644 --- a/cli/command/trust/common_test.go +++ b/cli/command/trust/common_test.go @@ -20,7 +20,7 @@ func TestMatchReleasedSignaturesSortOrder(t *testing.T) { rows := matchReleasedSignatures(targets) - var targetNames []string + targetNames := make([]string, 0, len(rows)) for _, r := range rows { targetNames = append(targetNames, r.SignedTag) } diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index aa1cd36b632b..3b769cec909a 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -710,7 +710,7 @@ func convertUlimits(origUlimits map[string]*composetypes.UlimitsConfig) []*units } } } - var ulimits []*units.Ulimit + ulimits := make([]*units.Ulimit, 0, len(newUlimits)) for _, ulimit := range newUlimits { ulimits = append(ulimits, ulimit) } diff --git a/cli/compose/convert/volume.go b/cli/compose/convert/volume.go index 257ddcd98d9d..387362203feb 100644 --- a/cli/compose/convert/volume.go +++ b/cli/compose/convert/volume.go @@ -12,14 +12,13 @@ type volumes map[string]composetypes.VolumeConfig // Volumes from compose-file types to engine api types func Volumes(serviceVolumes []composetypes.ServiceVolumeConfig, stackVolumes volumes, namespace Namespace) ([]mount.Mount, error) { - var mounts []mount.Mount - + mounts := make([]mount.Mount, 0, len(serviceVolumes)) for _, volumeConfig := range serviceVolumes { - mount, err := convertVolumeToMount(volumeConfig, stackVolumes, namespace) + mnt, err := convertVolumeToMount(volumeConfig, stackVolumes, namespace) if err != nil { return nil, err } - mounts = append(mounts, mount) + mounts = append(mounts, mnt) } return mounts, nil } diff --git a/cli/compose/loader/loader.go b/cli/compose/loader/loader.go index 203719f5095d..a1979bf5aee9 100644 --- a/cli/compose/loader/loader.go +++ b/cli/compose/loader/loader.go @@ -220,7 +220,7 @@ func GetUnsupportedProperties(configDicts ...map[string]interface{}) []string { } func sortedKeys(set map[string]bool) []string { - var keys []string + keys := make([]string, 0, len(set)) for key := range set { keys = append(keys, key) } @@ -394,7 +394,7 @@ func formatInvalidKeyError(keyPrefix string, key interface{}) error { // LoadServices produces a ServiceConfig map from a compose file Dict // the servicesDict is not validated if directly used. Use Load() to enable validation func LoadServices(servicesDict map[string]interface{}, workingDir string, lookupEnv template.Mapping) ([]types.ServiceConfig, error) { - var services []types.ServiceConfig + services := make([]types.ServiceConfig, 0, len(servicesDict)) for name, serviceDef := range servicesDict { serviceConfig, err := LoadService(name, serviceDef.(map[string]interface{}), workingDir, lookupEnv) diff --git a/cli/context/store/metadatastore.go b/cli/context/store/metadatastore.go index 5222897f3995..74ffd459cd91 100644 --- a/cli/context/store/metadatastore.go +++ b/cli/context/store/metadatastore.go @@ -106,7 +106,7 @@ func (s *metadataStore) list() ([]Metadata, error) { } return nil, err } - var res []Metadata + res := make([]Metadata, 0, len(ctxDirs)) for _, dir := range ctxDirs { c, err := s.getByID(contextdir(dir)) if err != nil { diff --git a/cli/context/store/store.go b/cli/context/store/store.go index 6042c2af7dba..2cad938da597 100644 --- a/cli/context/store/store.go +++ b/cli/context/store/store.go @@ -125,7 +125,7 @@ func Names(s Lister) ([]string, error) { if err != nil { return nil, err } - var names []string + names := make([]string, 0, len(list)) for _, item := range list { names = append(names, item.Name) } diff --git a/cli/manifest/types/types.go b/cli/manifest/types/types.go index 5b094f514219..ff7be969937d 100644 --- a/cli/manifest/types/types.go +++ b/cli/manifest/types/types.go @@ -52,8 +52,9 @@ func PlatformSpecFromOCI(p *ocispec.Platform) *manifestlist.PlatformSpec { // Blobs returns the digests for all the blobs referenced by this manifest func (i ImageManifest) Blobs() []digest.Digest { - digests := []digest.Digest{} - for _, descriptor := range i.SchemaV2Manifest.References() { + refs := i.SchemaV2Manifest.References() + digests := make([]digest.Digest, 0, len(refs)) + for _, descriptor := range refs { digests = append(digests, descriptor.Digest) } return digests diff --git a/opts/throttledevice.go b/opts/throttledevice.go index 0bf5dd666fdc..ed35d4c44271 100644 --- a/opts/throttledevice.go +++ b/opts/throttledevice.go @@ -60,9 +60,8 @@ type ThrottledeviceOpt struct { // NewThrottledeviceOpt creates a new ThrottledeviceOpt func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt { - values := []*blkiodev.ThrottleDevice{} return ThrottledeviceOpt{ - values: values, + values: []*blkiodev.ThrottleDevice{}, validator: validator, } } @@ -77,13 +76,13 @@ func (opt *ThrottledeviceOpt) Set(val string) error { } value = v } - (opt.values) = append((opt.values), value) + opt.values = append(opt.values, value) return nil } // String returns ThrottledeviceOpt values as a string. func (opt *ThrottledeviceOpt) String() string { - var out []string + out := make([]string, 0, len(opt.values)) for _, v := range opt.values { out = append(out, v.String()) } @@ -93,10 +92,9 @@ func (opt *ThrottledeviceOpt) String() string { // GetList returns a slice of pointers to ThrottleDevices. func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice { - var throttledevice []*blkiodev.ThrottleDevice - throttledevice = append(throttledevice, opt.values...) - - return throttledevice + out := make([]*blkiodev.ThrottleDevice, 0, len(opt.values)) + copy(out, opt.values) + return out } // Type returns the option type diff --git a/opts/ulimit.go b/opts/ulimit.go index 4667cc2544d4..5176b999a56e 100644 --- a/opts/ulimit.go +++ b/opts/ulimit.go @@ -34,7 +34,7 @@ func (o *UlimitOpt) Set(val string) error { // String returns Ulimit values as a string. Values are sorted by name. func (o *UlimitOpt) String() string { - var out []string + out := make([]string, 0, len(*o.values)) for _, v := range *o.values { out = append(out, v.String()) } @@ -44,7 +44,7 @@ func (o *UlimitOpt) String() string { // GetList returns a slice of pointers to Ulimits. Values are sorted by name. func (o *UlimitOpt) GetList() []*units.Ulimit { - var ulimits []*units.Ulimit + ulimits := make([]*units.Ulimit, 0, len(*o.values)) for _, v := range *o.values { ulimits = append(ulimits, v) } diff --git a/opts/weightdevice.go b/opts/weightdevice.go index f8057d0fb7dc..86f21e91bf03 100644 --- a/opts/weightdevice.go +++ b/opts/weightdevice.go @@ -59,13 +59,13 @@ func (opt *WeightdeviceOpt) Set(val string) error { } value = v } - (opt.values) = append((opt.values), value) + opt.values = append(opt.values, value) return nil } // String returns WeightdeviceOpt values as a string. func (opt *WeightdeviceOpt) String() string { - var out []string + out := make([]string, 0, len(opt.values)) for _, v := range opt.values { out = append(out, v.String()) }