10 examples of 'time golang' in Go

Every line of 'time golang' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
8func main() {
9 timeAsString := "2012-01-22"
10 fmt.Println(time.Parse("2006-01-01_this-does-not-compile", timeAsString))
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1152func (s *Delete_commandContext) Time() ITimeContext {
1153 var t = s.GetTypedRuleContext(reflect.TypeOf((*ITimeContext)(nil)).Elem(), 0)
1154
1155 if t == nil {
1156 return nil
1157 }
1158
1159 return t.(ITimeContext)
1160}
6func main() {
7 // Create new date.
8 a := time.Date(2018, 4, 11, 0, 0, 0, 0, time.UTC)
9
10 delta := time.Now().Sub(a)
11 fmt.Println(delta.Hours())
12
13 duration := time.Since(a)
14 fmt.Println(duration.Hours())
15}
8func main() {
9 t := time.Date(2009, time.November, 10, 23, 4, 5, 0, time.UTC)
10 h, m, s := t.Clock()
11 fmt.Println(h, m, s)
12}
18func main() {
19 john := person{
20 name: "John Coltrane",
21 dob: time.Date(1926, time.September, 23, 0, 0, 0, 0, time.UTC),
22 }
23 print(john)
24 noodles := product{
25 name: "noodles",
26 cost: 235,
27 }
28 print(noodles)
29 print("a string?")
30}
15func timeHandler(w http.ResponseWriter, r *http.Request) {
16 t := time.Now().Format(time.RFC1123)
17 Body := "The current time is:"
18 fmt.Fprintf(w, "<h1 align=\"center\">%s</h1>", Body)
19 fmt.Fprintf(w, "<h2 align=\"center\">%s</h2>\n", t)
20 fmt.Fprintf(w, "Serving: %s\n", r.URL.Path)
21 fmt.Printf("Served time for: %s\n", r.Host)
22}
60func timeBridge(b *bridgeConn) {
61 go func() {
62 t := time.Duration(rand.Intn(5)+1) * time.Second
63 time.Sleep(t)
64 log.Printf("killing connection %s after %v\n", b.String(), t)
65 b.Close()
66 }()
67 bridge(b)
68}
22func main() {
23 fmt.Print("APP-STARTED!\n")
24 for {
25 fmt.Printf("%s\n", time.Now().Format("Mon Jan 2 15:04:05 -0700 MST 2006"))
26 }
27}
39func main() {
40
41 // Use the time package to get the current date/time.
42 now := time.Now()
43
44 // Subtract 5 nanoseconds from now time using a literal constant.
45 lessFiveNanoseconds := now.Add(-5)
46
47 // Subtract 5 seconds from now using a declared constant.
48 lessFiveSeconds := now.Add(-fiveSeconds)
49
50 // Display the values.
51 fmt.Printf("Now : %v\n", now)
52 fmt.Printf("Nano : %v\n", lessFiveNanoseconds)
53 fmt.Printf("Seconds : %v\n", lessFiveSeconds)
54}
176func goTime(t C.pn_timestamp_t) time.Time {
177 if t == 0 {
178 return time.Time{}
179 }
180 return time.Unix(0, int64(t)*int64(time.Millisecond))
181}

Related snippets