error.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2011 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. // This file contains the code snippets included in "Error Handling and Go."
  5. package main
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "log"
  11. "net"
  12. "os"
  13. "time"
  14. )
  15. type File struct{}
  16. func Open(name string) (file *File, err error) {
  17. // OMIT
  18. panic(1)
  19. // STOP OMIT
  20. }
  21. func openFile() { // OMIT
  22. f, err := os.Open("filename.ext")
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. // do something with the open *File f
  27. // STOP OMIT
  28. _ = f
  29. }
  30. // errorString is a trivial implementation of error.
  31. type errorString struct {
  32. s string
  33. }
  34. func (e *errorString) Error() string {
  35. return e.s
  36. }
  37. // STOP OMIT
  38. // New returns an error that formats as the given text.
  39. func New(text string) error {
  40. return &errorString{text}
  41. }
  42. // STOP OMIT
  43. func Sqrt(f float64) (float64, error) {
  44. if f < 0 {
  45. return 0, errors.New("math: square root of negative number")
  46. }
  47. // implementation
  48. return 0, nil // OMIT
  49. }
  50. // STOP OMIT
  51. func printErr() (int, error) { // OMIT
  52. f, err := Sqrt(-1)
  53. if err != nil {
  54. fmt.Println(err)
  55. }
  56. // STOP OMIT
  57. // fmtError OMIT
  58. if f < 0 {
  59. return 0, fmt.Errorf("math: square root of negative number %g", f)
  60. }
  61. // STOP OMIT
  62. return 0, nil
  63. }
  64. type NegativeSqrtError float64
  65. func (f NegativeSqrtError) Error() string {
  66. return fmt.Sprintf("math: square root of negative number %g", float64(f))
  67. }
  68. // STOP OMIT
  69. type SyntaxError struct {
  70. msg string // description of error
  71. Offset int64 // error occurred after reading Offset bytes
  72. }
  73. func (e *SyntaxError) Error() string { return e.msg }
  74. // STOP OMIT
  75. func decodeError(dec *json.Decoder, val struct{}) error { // OMIT
  76. var f os.FileInfo // OMIT
  77. if err := dec.Decode(&val); err != nil {
  78. if serr, ok := err.(*json.SyntaxError); ok {
  79. line, col := findLine(f, serr.Offset)
  80. return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
  81. }
  82. return err
  83. }
  84. // STOP OMIT
  85. return nil
  86. }
  87. func findLine(os.FileInfo, int64) (int, int) {
  88. // place holder; no need to run
  89. return 0, 0
  90. }
  91. func netError(err error) { // OMIT
  92. for { // OMIT
  93. if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
  94. time.Sleep(1e9)
  95. continue
  96. }
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. // STOP OMIT
  101. }
  102. }
  103. func main() {}