esercizi 12 dic 2019

parent 32957cc5
This diff is collapsed.
package main
import (
"fmt"
"reflect"
)
func main() {
var s []int
//s=[]int{}
fmt.Println(s, len(s), cap(s), reflect.TypeOf(s))
if s == nil {
fmt.Println("nil!")
}
}
package main
import "fmt"
func main() {
// Create an empty slice of slices.
animals := [][]string{}
// Create three string slices.
row1 := []string{"fish", "shark", "eel"}
row2 := []string{"bird"}
row3 := []string{"lizard", "salamander"}
// Append string slices to outer slice.
animals = append(animals, row1)
animals = append(animals, row2)
animals = append(animals, row3)
// Loop over slices in animals.
for i := range animals {
fmt.Printf("Row: %v\n", i)
fmt.Println(animals[i])
}
}
//scrivere un programma che appenda elementi ad una slice, e che stampi a distanza di un intervallo di tempo
//i vari valori che la slice assume nel tempo
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var s []int
now := time.Now().UnixNano()
rand.Seed(now)
//d,_=time.ParseDuration("1.5s")
//fmt.Println(s)
for i:=0; i<10000; i++ {
//n:=rand.Int()%100
s=append(s,155)
fmt.Print(len(s),cap(s),": ");
fmt.Println(time.Now().UnixNano()-now)
now = time.Now().UnixNano()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment