notexOPENOverview
Notex/Languages/Practice: Using Pointers, Slices, and Maps
04.13LESSON 13 OF 20 · 1 MIN READ

Practice: Using Pointers, Slices, and Maps

Always free to read

These exercises let you see how pointers and slices interact with function calls.

  1. Write two functions: double(n int) int and doubleInPlace(n *int). Call both from main and explain the differences in their behavior. Which approach is better and why?

  2. Define a struct with a Count *int field tagged for JSON. Unmarshal {"count":0} and {} into it. Print “count is zero” when the field is set to zero and “count is absent” when the field is empty.

  3. Write a function called changeSlice([]int) that takes in a slice and doubles the value of every element in it. Call this function from main with the slice vals := []int{1,2,3} and print out the result. Guess what the result will be before running the program. What was your guess? Was it correct? Then write a second function called growSlice([]int) that takes in a slice and appends 10 to the slice until it is double its original length. Guess what the result will be before running the program. Print out the result. Was your guess correct? Why or why not?

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K