add: slices advanced ops

This commit is contained in:
Inanc Gumus
2019-02-28 13:25:12 +03:00
parent 33add371bd
commit 330abbce5a
11 changed files with 4 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
// 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"
s "github.com/inancgumus/prettyslice"
)
func main() {
// #1: received the raining probabilities
data := []float64{10, 25, 30, 50}
// #2: received new data
// newData := []float64{80, 90}
// for i := 0; i < len(newData); i++ {
// data[i] = newData[i]
// }
// #3: use copy
// copy(data, []float64{99, 100})
// #4: received more data than the original
// copy(data, []float64{10, 5, 15, 0, 20})
// #5: returns the # of copied elements
// n := copy(data, []float64{10, 5, 15, 0, 20})
// fmt.Printf("%d probabilities copied.\n", n)
// #6: (sometimes) use append instead of copy
// data = append(data[:0], []float64{10, 5, 15, 0, 20}...)
// #7: clone a slice using copy
// saved := make([]float64, len(data))
// copy(saved, data)
// #9: clone a slice using append nil (i prefer this)
// saved := append([]float64(nil), data...)
// data[0] = 0 // #8
// s.Show("Probabilities (saved)", saved) // #7
// #1: print the probabilities
s.Show("Probabilities (data)", data)
fmt.Printf("Is it gonna rain? %.f%% chance.\n",
(data[0]+data[1]+data[2]+data[3])/
float64(len(data)))
}