3-1 defer调用.mp4

  1. func writeFile(filename string){
  2. file,err:=os.Create(filename)
  3. if err != nil {
  4. panic(err)
  5. }
  6. defer file.Close()
  7. writer:= bufio.NewWriter(file)
  8. defer writer.Flush()
  9. for i := 0; i < 20; i++ {
  10. fmt.Fprintln(writer,string(i))
  11. }
  12. }

等我代码写完了,我就忘记这个了
所以我就进就加一个这个
第3章_错误处理和资源管理 - 图1

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func tryDefer() {
  8. defer fmt.Println(2)
  9. defer fmt.Println(1)
  10. fmt.Println(3)
  11. fmt.Println(4)
  12. }
  13. func writeFile(filename string) {
  14. file, err := os.Create(filename)
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer file.Close()
  19. writer := bufio.NewWriter(file)
  20. defer writer.Flush()
  21. for i := 0; i < 20; i++ {
  22. fmt.Fprintln(writer, i)
  23. }
  24. }
  25. func main() {
  26. writeFile("fib.txt")
  27. }

3-2 错误处理概念.mp4

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func tryDefer() {
  8. defer fmt.Println(2)
  9. defer fmt.Println(1)
  10. fmt.Println(3)
  11. fmt.Println(4)
  12. }
  13. func writeFile(filename string) {
  14. file, err := os.Create(filename)
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer file.Close()
  19. writer := bufio.NewWriter(file)
  20. defer writer.Flush()
  21. for i := 0; i < 20; i++ {
  22. fmt.Fprintln(writer, i)
  23. }
  24. }
  25. func writeFile2(filename string) {
  26. file, err := os.OpenFile(
  27. filename, os.O_EXCL|os.O_CREATE, 0666)
  28. if err != nil {
  29. panic(err)
  30. }
  31. defer file.Close()
  32. writer := bufio.NewWriter(file)
  33. defer writer.Flush()
  34. for i := 0; i < 20; i++ {
  35. fmt.Fprintln(writer, i)
  36. }
  37. }
  38. func main() {
  39. writeFile2("fib.txt")
  40. /**
  41. panic: open fib.txt: The file exists.
  42. goroutine 1 [running]:
  43. main.writeFile2({0xf7b58, 0xc000040000})
  44. E:/Projects/GolandProjects/go-camp/mooc/code/learngo/errhandling/defer/defer.go:35 +0x1d5
  45. main.main()
  46. E:/Projects/GolandProjects/go-camp/mooc/code/learngo/errhandling/defer/defer.go:47 +0x25
  47. Process finished with the exit code 2
  48. */
  49. }

如果文件存在就报错

可以处理一下

  1. func writeFile2(filename string) {
  2. file, err := os.OpenFile(
  3. filename, os.O_EXCL|os.O_CREATE, 0666)
  4. if err != nil {
  5. fmt.Println("flie already exist!!!")
  6. return
  7. }
  8. defer file.Close()
  9. writer := bufio.NewWriter(file)
  10. defer writer.Flush()
  11. for i := 0; i < 20; i++ {
  12. fmt.Fprintln(writer, i)
  13. }
  14. }

真正的处理错误

  1. func writeFile2(filename string) {
  2. file, err := os.OpenFile(
  3. filename, os.O_EXCL|os.O_CREATE, 0666)
  4. if err != nil {
  5. if pathError, ok := err.(*os.PathError); !ok {
  6. // 真的 不知道是森什么错了
  7. // 那就挂掉程序吧
  8. panic(err)
  9. } else {
  10. fmt.Printf("%s, %s, %s\n",
  11. pathError.Op,
  12. pathError.Path,
  13. pathError.Err,
  14. )
  15. }
  16. return
  17. }
  18. defer file.Close()
  19. writer := bufio.NewWriter(file)
  20. defer writer.Flush()
  21. for i := 0; i < 20; i++ {
  22. fmt.Fprintln(writer, i)
  23. }
  24. }

3-3 服务器统一出错处理浏览器需放大.mp4

3-4 服务器统一出错处理.mp4

error vs panic

  • 意料之中: 使用error . 如文件打不开
  • 意料之外: 使用panic . 如数组越界

第3章_错误处理和资源管理 - 图2
输入的参数类型是函数
输出的返回类型也是函数