2019-03-04 20:30:18 +03:00
|
|
|
// For more tutorials: https://blog.learngoprogramming.com
|
|
|
|
//
|
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-03-05 23:32:32 +03:00
|
|
|
"github.com/inancgumus/learngo/16-slices/exercises/23-limit-the-backing-array-sharing/api"
|
2019-03-04 20:30:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
// EXERCISE: Limit the backing array sharing
|
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// You've created a package and saved the temperatures in an int slice.
|
|
|
|
// (find it in the api folder).
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// When `Read()` function is called, you return the desired elements.
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// Another programmer has decided to use your package.
|
|
|
|
// She calls: `api.Read(0, 3)` and gets a slice with a length of 3.
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// However: She appends to the slice, so she practically changes
|
|
|
|
// the elements in the api package's `temps` slice (the original one).
|
|
|
|
//
|
|
|
|
// Only allow her to change the first three elements.
|
|
|
|
// Prevent her from changing the rest of the elements of
|
|
|
|
// `api.temps` slice.
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// STEPS
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// You only need to change the code inside the `api/api.go` folder
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// CURRENT
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
|
|
|
// | |
|
2019-08-18 12:59:50 +03:00
|
|
|
// v v
|
|
|
|
// api.temps : [5 10 3 1 3 80 90]
|
|
|
|
// main.temps : [5 10 3 1 3]
|
|
|
|
// ^ ^ append changes the api package's
|
|
|
|
// temps slice: [1 3]
|
|
|
|
//
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// EXPECTED
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
2019-08-18 12:59:50 +03:00
|
|
|
// Now the program cannot change the API's original backing array.
|
2019-03-04 20:30:18 +03:00
|
|
|
// | |
|
2019-08-18 12:59:50 +03:00
|
|
|
// v v
|
|
|
|
// api.temps : [5 10 3 25 45 80 90]
|
|
|
|
// main.temps : [5 10 3 1 3]
|
2019-03-04 20:30:18 +03:00
|
|
|
//
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// DO NOT TOUCH THE FOLLOWING CODE
|
|
|
|
// THIS IS THE CLIENT PROGRAM THAT USES YOUR API
|
|
|
|
// YOU CANNOT CONTROL IT! :)
|
|
|
|
|
2019-08-18 12:59:50 +03:00
|
|
|
// get the first three elements from api.temps
|
2019-03-04 20:30:18 +03:00
|
|
|
temps := api.Read(0, 3)
|
|
|
|
|
2019-08-18 12:59:50 +03:00
|
|
|
// changes the api.temps's backing array.
|
|
|
|
// you need to prevent this.
|
2019-03-04 20:30:18 +03:00
|
|
|
temps = append(temps, []int{1, 3}...)
|
|
|
|
|
2019-08-18 12:59:50 +03:00
|
|
|
fmt.Println("api.temps :", api.All())
|
|
|
|
fmt.Println("main.temps :", temps)
|
2019-03-04 20:30:18 +03:00
|
|
|
}
|