array-slice-passing

command module
v0.0.0-...-838b099 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 1 Imported by: 0

README

Passing Array to Functions

In Go, arrays are passed to a function as values. So the receiving function gets a copy of the original array. This can be a problem if we need to change the array. To modify the original array, we need to pass the array as a reference or use a slice.

A slice, on the hand, isn't a data structure but rather a variable that references the original data structure. So whether you pass a slice (as a value or reference) to a function, only a copy of that variable is made, which still points to the original data structure.

Notes

It's interesting to note that a slice has a different set of rules when it being passed to a function. The receiving function gets a copy of the original slice, but it still references the original array. This is due to the fact that slice is a struct consisting of a pointer to the array, length of the segment, and its capacity. See Go Blog: Slice Intro for details.

Setup

  1. Run the program.

    $ make run
    

Reference

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL