time Package का उपयोग
now := time.Now()
d := time.Duration(n) * time.Second
t.Format("2006-01-02")
time.Sleep(d)
Getting the Current Time
time.Now() current moment को represent करती एक time.Time value return करता है, जो एक पूरी date, time, और time zone रखती है, और formatting व arithmetic को support करती है।
उदाहरण: Getting the Current Time
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"time"
)
// main is where the program starts running
func main() {
// Short declaration: create now and infer the type
now := time.Now()
// Print the values followed by a newline
fmt.Println("current year:", now.Year())
}
Login to try C/C++/Java/PHP code in the editor
Measuring Elapsed Time
time.Since(start) एक time.Duration return करता है जो बताता है कि एक recorded start time के बाद से कितना समय बीता -- किसी operation में कितना समय लगता है यह measure करने का standard तरीका।
उदाहरण: Measuring Elapsed Time
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"time"
)
// main is where the program starts running
func main() {
// Short declaration: create start and infer the type
start := time.Now()
// Short declaration: create sum and infer the type
sum := 0
// Loop: repeat while the condition holds
for i := 0; i < 1000000; i++ {
sum += i
}
elapsed := time.Since(start)
// Print the values followed by a newline
fmt.Println("sum:", sum, "took at least 0ns:", elapsed >= 0)
}
Login to try C/C++/Java/PHP code in the editor
Working with Durations
एक time.Duration समय के एक span को represent करता है और आमतौर पर time.Second या time.Millisecond जैसे constants से बनाया जाता है, जो sleeps और timeouts जैसे duration-based code को self-documenting बनाता है।
उदाहरण: Working with Durations
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"time"
)
// main is where the program starts running
func main() {
// Short declaration: create d and infer the type
d := 2 * time.Second
// Print the values followed by a newline
fmt.Println("duration:", d)
time.Sleep(1 * time.Millisecond)
// Print the values followed by a newline
fmt.Println("slept briefly")
}
Login to try C/C++/Java/PHP code in the editor
- internal monotonic clock readings के कारण Sub/Before/After/Equal ज़्यादा सही होने के बावजूद दो time.Time values को == से compare करना।
- यह भूल जाना कि time.Sleep पूरे goroutine को रोकता है, यह बाकी जो भी कर रहा हो उसे block करते हुए।
- स्पष्टता के लिए time.Second जैसे constants उपयोग करने के बजाय किसी time.Duration की numeric value (nanoseconds) को एक human unit के साथ confuse करना।
- time.Now current local time को एक time.Time value के रूप में return करता है।
- time.Duration समय की एक लंबाई को represent करता है, time.Second या time.Millisecond जैसे constants से express किया गया।
- time.Since यह compute करता है कि किसी दिए time.Time के बाद से कितना समय बीता।
- time.Sleep current goroutine को एक दी गई duration के लिए रोकता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: