Black Lives Matter. Support the Equal Justice Initiative.

Source file src/errors/example_test.go

Documentation: errors

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package errors_test
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  )
    11  
    12  // MyError is an error implementation that includes a time and message.
    13  type MyError struct {
    14  	When time.Time
    15  	What string
    16  }
    17  
    18  func (e MyError) Error() string {
    19  	return fmt.Sprintf("%v: %v", e.When, e.What)
    20  }
    21  
    22  func oops() error {
    23  	return MyError{
    24  		time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
    25  		"the file system has gone away",
    26  	}
    27  }
    28  
    29  func Example() {
    30  	if err := oops(); err != nil {
    31  		fmt.Println(err)
    32  	}
    33  	// Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away
    34  }
    35  

View as plain text