Arpssss
Arpssss

Reputation: 3858

Go Programming Language - System Time

Can anybody help me by answering how to get system time in GO. For example, in the below code, I want to know when the first print and second print statements are executed and what is the time difference between them.

package main

import "fmt"

func main() {
fmt.Println("Hello, 世界")
    fmt.Println("Hello, 世界")
}

Upvotes: 2

Views: 909

Answers (2)

Kissaki
Kissaki

Reputation: 9237

See the time package. Documentation can be found at http://golang.org/pkg/time/

For getting the nanoseconds-time before, after the first and after the last of your statements, and then printing them out:

package main

import "fmt"
import "time"

func main() {
  i1 := time.Nanoseconds();
  fmt.Println("Hello, 世界")
  i2 := time.Nanoseconds();
  fmt.Println("Hello, 世界")
  i3 := time.Nanoseconds();
  fmt.Println("Check this out!")
  fmt.Println(i1)
  fmt.Println(i2)
  fmt.Println(i3)
  fmt.Println(time.NanosecondsToLocalTime(i1).Format(time.StampNano))
  fmt.Println(time.NanosecondsToLocalTime(i2).Format(time.StampNano))
  fmt.Println(time.NanosecondsToLocalTime(i3).Format(time.StampNano))
}

In my VM, I then get the following results:

~/dev/go/test $ nano test.go
~/dev/go/test $ 6g test.go; 
~/dev/go/test $ 6l test.6
~/dev/go/test $ ./6.out
Hello, 世界
Hello, 世界
Check this out!
1322386593830456000
1322386593830498000
1322386593830501000
Nov 27 10:36:33.830456000
Nov 27 10:36:33.830498000
Nov 27 10:36:33.830501000

Upvotes: 5

Jamison Dance
Jamison Dance

Reputation: 20184

See the time package. http://golang.org/pkg/time/

Upvotes: 1

Related Questions