add: slice adv. ops. exercises

This commit is contained in:
Inanc Gumus
2019-03-05 23:32:32 +03:00
parent e379976af4
commit 9c96082326
19 changed files with 810 additions and 99 deletions

View File

@@ -0,0 +1,24 @@
package api
var temps = []int{5, 10, 3, 25, 45, 80, 90}
// Read returns a range of temperature readings beginning from
// the `start` until to the `stop`.
func Read(start, stop int) []int {
// Uses a full slice expression to control the length of the
// backing array (or the capacity of the returned slice).
//
// So the next append allocates a new backing array, which
// in turn doesn't overwrite the temps slice's backing array.
// ^^
// ||
// / \
// | |
portion := temps[start:stop:stop]
return portion
}
// All returns all the temperature readings
func All() []int {
return temps
}

View File

@@ -0,0 +1,22 @@
// 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"
"github.com/inancgumus/learngo/16-slices/exercises/23-limit-the-backing-array-sharing/solution/api"
)
func main() {
temps := api.Read(0, 3)
temps = append(temps, []int{1, 3}...)
fmt.Println("API's readings:", api.All())
fmt.Println("Your readings :", temps)
}