package main
import "fmt"
import "time"
func main() {
fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());
fmt.Println("sleep 1 second.");
time.Sleep(1*1000*1000*1000);
fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());
fmt.Println("wait sleep 1 second.");
before := time.Nanoseconds();
time.Sleep(1*1000*1000*1000);
after := time.Nanoseconds();
fmt.Println("difference of nanoseconds: ", (after-before));
t:=time.LocalTime();
fmt.Println("time.String(): ", t.String());
}
time.Seconds() get the time as unix time in seconds
time.Nanoseconds() dito
time.Sleep(x) sleep for x nanoseconds
to sleep 1 second do time.Sleep(1e9)
to get any type back from function
let look:
package main
import "fmt"
import "rand"
import "time"
func my() interface{} {
rand.Seed(time.Nanoseconds()/1000000);
switch rand.Intn(3) {
case 0: var i int; i=4; return i;
case 1: var f float; f=2.3; return f;
case 2: var s string; s="blub"; return s;
}
var b byte;
b='c';
return b;
}
func main() {
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
}
with func my() interface{} {...} we declare a function which return an interface. This is a void interface, which can implement all types. Thus we can back every typ back. The my() function simple init the rand and get randomly different types back.
Keine Kommentare:
Kommentar veröffentlichen