DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

Arrays vs. Slices in Go: What Beginners Need to Know

Go arrays are fixed-size values; slices describe shared segments of arrays. Understand copying, len, cap, append, and when to copy slice data.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An array is a fixed-size value; a slice is a small descriptor for a segment of an underlying array. Copying an array copies its elements. Copying a slice copies only the descriptor, so slices can share and change the same elements. That distinction explains len, cap, and why you must assign the result of append.

What’s the difference between an array and a slice in Go?

Property Array Slice
What it is A fixed number of elements; its length is part of its type. A descriptor for a segment of an underlying array.
Length Fixed by the array’s type. Can change when you reslice or use the result of append.
Assignment or passing by value Copies the array’s elements. Copies the descriptor, not the backing array’s elements.
Storage sharing A copied array has its own elements; a pointer to an array can provide shared access. Multiple slices can refer to the same backing array and observe changes to shared elements.

Array length is part of the type: [3]int and [4]int are different types. An array cannot grow; if code needs a variable-length sequence, a slice is usually the more suitable type. See the Go specification’s array and slice definitions.

As an Amazon Associate I earn from qualifying purchases.

Are slices just dynamic arrays?

Not exactly. A slice is not an array that resizes in place. It describes a view into an array, including how many elements are currently in the view and how far it can extend. When you append, Go returns a slice describing the result; that result may still use the old array or may use new storage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does slicing copy the underlying array?

No. Creating a slice from an array or reslicing an existing slice does not copy its elements. The slice refers to the original array’s storage, so changing an element through one view can be seen through another view that covers that element.

arr := [3]int{1, 2, 3}
copyOfArr := arr
copyOfArr[0] = 9
// arr[0] is still 1

s := arr[:]
s[0] = 7
// arr[0] is now 7: s refers to arr's storage

s = append(s, 4) // keep the returned slice

The array assignment makes an independent array value. By contrast, arr[:] makes a slice view over arr. The last line keeps the slice returned by append; whether it shares the original backing array depends on available capacity.

The same distinction matters when passing values to functions: passing an array by value passes a copy, while passing a slice copies the slice descriptor and leaves its backing array shared. A function that changes an element through its slice argument can therefore affect the caller’s data. For the specification’s definitions and the Go Blog’s explanation of slice sharing, see the Go specification and Go Slices: usage and internals.

What’s the difference between len and cap?

  • len(s) is the number of elements currently in the slice. Those are the elements you can index.
  • cap(s) is how far the slice can extend from its start into its backing array. It describes room for extension, not the number of elements currently available for indexing.

For example, make([]int, 2, 5) creates a slice with length 2 and capacity 5. Its valid indices are 0 and 1; indexing at 2 is invalid even though the capacity is 5. You can extend it up to its capacity by reslicing, or add elements with append. You cannot reslice beyond capacity. The specification describes slice length and capacity, while the built-in documentation for make explains how to create a slice with a chosen length and optional capacity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why do I need to assign the result of append?

append returns the slice describing the appended result. Always use that returned slice, commonly by assigning it back to the same variable:

s = append(s, value)

If there is room in the slice’s capacity, the result can continue to use the existing backing array. If more storage is needed, the result can refer to a new backing array. The language does not guarantee a particular growth factor or allocation strategy, so code should not depend on one. The Go specification defines the operation; Rob Pike’s explanation of append mechanics is useful background, but its historical growth example is not a current runtime contract.

Because the result may share storage or use new storage, do not assume that appending always changes the same array—or always creates a new one. Keep the returned slice and avoid relying on which storage it uses.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should I copy slice elements?

Sharing a backing array can be efficient, but it is not always what you want. Copy elements when the new slice must be independent, or when a small, long-lived slice should not keep a much larger backing array reachable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The built-in copy copies elements between slices, up to the shorter of their lengths. One way to create independent storage is to allocate a destination and copy the required elements:

small := large[start:end]
independent := make([]int, len(small))
copy(independent, small)

independent has its own backing storage for the copied elements. The original slice and its array may still exist elsewhere, but retaining independent alone does not require retaining that large array. See the built-in documentation for copy and make and the Go Blog’s example of a small slice retaining a larger array in Go Slices: usage and internals.

Can I append to a nil slice?

Yes. A nil slice is a valid zero-value slice, and append can return a non-empty slice from it:

var s []int
s = append(s, 1)

As with any append, use the returned value. The specification defines append for slices, including nil slices: Go specification.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A simple way to predict what happens

  • Array assignment: another array value gets a copy of the elements.
  • Slice creation or assignment: another descriptor can refer to the same elements.
  • Indexing: valid only within the slice’s current length.
  • Reslicing: can extend within capacity, but not beyond it.
  • Appending: returns the slice to keep; its backing storage may be shared or new.
  • Independent data: allocate and use copy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.