Home >Backend Development >Golang >How to Correctly Set Timeout Duration in Go Test?

How to Correctly Set Timeout Duration in Go Test?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-07 04:12:11394browse

How to Correctly Set Timeout Duration in Go Test?

Timeout Duration Setting in Go Test

In a typical Go test workflow, users may encounter an error when setting the timeout flag using a simple numeric value:

go test -timeout 99999

This command will result in the following error:

invalid value "99999" for flag -test.timeout: time: missing unit in duration 99999

Solution: Using Valid Duration Format

To rectify this issue, users should utilize a valid time format as specified by time.ParseDuration. Valid formats include:

  • "h" or "H" for hours
  • "m" or "M" for minutes
  • "s" or "S" for seconds
  • "ms" or "µs" for milliseconds

Examples:

$ go test -timeout 300ms

$ go test -timeout 99999s

Reference:

  • [Package flag: Duration Flags](https://golang.org/pkg/flag/#hdr-Duration_Flags)
  • [Package time: ParseDuration](https://golang.org/pkg/time/#ParseDuration)

The above is the detailed content of How to Correctly Set Timeout Duration in Go Test?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn