Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

YouTube

Passing Slices to Functions in Go - Reference Semantics

Gary Clarke via YouTube

Overview

Coursera Flash Sale
40% Off Coursera Plus for 3 Months!
Grab it
Learn why slices in Go behave differently than you might expect when passed to functions in this 11-minute tutorial. Discover how Go's slice reference semantics work, where passing a slice to a function copies only the slice header (containing pointer, length, and capacity) rather than the underlying data array. Understand why modifications to slice elements inside functions affect the original slice in the calling code, and explore what happens when appending to a slice either reuses existing capacity or triggers creation of a new backing array. Master the concept of shared backing arrays and how slice headers point to the same underlying storage, making slice operations efficient by avoiding unnecessary data copying. Examine scenarios where append operations may or may not create new arrays depending on available capacity, and learn when slices stop sharing data. Gain practical knowledge for writing efficient helper functions that process or update slice data, while avoiding common pitfalls where changes appear in unexpected places. Practice with a hands-on challenge to reinforce understanding of slice reference semantics, backing arrays, and the relationship between slice headers and underlying data storage.

Syllabus

Passing Slices to Functions in Go (Reference Semantics)

Taught by

Gary Clarke

Reviews

Start your review of Passing Slices to Functions in Go - Reference Semantics

Never Stop Learning.

Get personalized course recommendations, track subjects and courses with reminders, and more.

Someone learning on their laptop while sitting on the floor.