Introduction
This lab aims to test your understanding of Go's time and duration support.
Time
The code below contains examples of how to work with time and duration in Go. However, some parts of the code are missing. Your task is to complete the code to make it work as expected.
- Basic knowledge of Go programming language.
- Familiarity with Go's time and duration support.
$ go run time.go 2012-10-31 15:50:13.793654 +0000 UTC 2009-11-17 20:34:58.651387237 +0000 UTC 2009 November 17 20 34 58 651387237 UTC Tuesday true false false 25891h15m15.142266763s 25891.25420618521 1.5534752523711128e+06 9.320851514226677e+07 93208515142266763 2012-10-31 15:50:13.793654 +0000 UTC 2006-12-05 01:19:43.509120474 +0000 UTC # Next we'll look at the related idea of time relative to # the Unix epoch.
There is the full code below:
// Go offers extensive support for times and durations; // here are some examples. package main import ( "fmt" "time" ) func main() { p := fmt.Println // We'll start by getting the current time. now := time.Now() p(now) // You can build a `time` struct by providing the // year, month, day, etc. Times are always associated // with a `Location`, i.e. time zone. then := time.Date( 2009, 11, 17, 20, 34, 58, 651387237, time.UTC) p(then) // You can extract the various components of the time // value as expected. p(then.Year()) p(then.Month()) p(then.Day()) p(then.Hour()) p(then.Minute()) p(then.Second()) p(then.Nanosecond()) p(then.Location()) // The Monday-Sunday `Weekday` is also available. p(then.Weekday()) // These methods compare two times, testing if the // first occurs before, after, or at the same time // as the second, respectively. p(then.Before(now)) p(then.After(now)) p(then.Equal(now)) // The `Sub` methods returns a `Duration` representing // the interval between two times. diff := now.Sub(then) p(diff) // We can compute the length of the duration in // various units. p(diff.Hours()) p(diff.Minutes()) p(diff.Seconds()) p(diff.Nanoseconds()) // You can use `Add` to advance a time by a given // duration, or with a `-` to move backwards by a // duration. p(then.Add(diff)) p(then.Add(-diff)) }
Summary
This lab tested your ability to work with Go's time and duration support. You learned how to extract various components of a time value, compare two times, compute the length of a duration, and advance a time by a given duration.
? Practice Now: Go Time and Duration Exploration
Want to Learn More?
- ? Learn the latest Go Skill Trees
- ? Read More Go Tutorials
- ? Join our Discord or tweet us @WeAreLabEx
The above is the detailed content of Go Time and Duration | Programming Tutorials. For more information, please follow other related articles on the PHP Chinese website!

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization

The article discusses Go's reflect package, used for runtime manipulation of code, beneficial for serialization, generic programming, and more. It warns of performance costs like slower execution and higher memory use, advising judicious use and best

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.

The article discusses using table-driven tests in Go, a method that uses a table of test cases to test functions with multiple inputs and outcomes. It highlights benefits like improved readability, reduced duplication, scalability, consistency, and a


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
