add: slice internals backing array and slice header

This commit is contained in:
Inanc Gumus
2019-02-08 13:10:55 +03:00
parent ff4eb232d1
commit 04c5e2bec7
16 changed files with 642 additions and 36 deletions

View File

@ -0,0 +1,60 @@
// 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"
"math/rand"
"time"
)
// ---------------------------------------------------------
// EXERCISE: Fix the backing array problem
//
// You receive numbers from an API. After you're done working
// with it, the API needs to continue using those numbers.
//
// But your program changes the numbers (changes the API's slice).
//
// Fix the program so that your program doesn't modify
// the original numbers.
//
//
// RESTRICTION
//
// Fix your problem only in the designated area of the code below.
//
//
// EXPECTED OUTPUT
//
// Mine : [-50 -100 -150]
// Original nums: [56 89 15]
//
// Note: Original nums may vary (they're random)
// But your slice should look like the above (mine slice)
//
// Yes, it should output only three numbers for the both slices!
//
// ---------------------------------------------------------
func main() {
// API returns random numbers in an int slice
rand.Seed(time.Now().UnixNano())
nums := rand.Perm(100)
// ----------------------------------------
// RESTRICTIONS — ONLY ADD YOUR CODE HERE
//
mine := nums
//
// ----------------------------------------
mine[0], mine[1], mine[2] = -50, -100, -150
fmt.Println("Mine :", mine)
fmt.Println("Original nums:", nums[:3])
}