Standard library / os

os.File.SetReadDeadline()

Sets an absolute deadline for current and future reads on supported handles; zero disables it. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) SetReadDeadline(t time.Time) error

Parameters & Attributes

NameTypeDescription
ttime.TimeAbsolute deadline; zero clears it.

Return Value

error

Sets an absolute deadline for current and future reads on supported handles; zero disables it.

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt"  "os"  "time")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  r, w, e := os.Pipe()  check(e)  defer r.Close()  defer w.Close()  check(r.SetReadDeadline(time.Now().Add(-time.Second)))  _, e = r.Read(make([]byte, 1))  fmt.Println(errors.Is(e, os.ErrDeadlineExceeded))}
Go Reference