From ef9d11a57d7ccbf2377440ea2e81a5e4017315ee Mon Sep 17 00:00:00 2001 From: akorastelev Date: Mon, 12 Apr 2021 16:12:24 +0300 Subject: [PATCH] add lesson_1 --- lesson1/example1/main.go | 54 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/lesson1/example1/main.go b/lesson1/example1/main.go index 6cd19c0..1d77a5c 100644 --- a/lesson1/example1/main.go +++ b/lesson1/example1/main.go @@ -1 +1,53 @@ -package example1 +package main + +import ( + "fmt" + "os" + "time" +) + +func main() { + err := thisIsPanic() + if err != nil { + fmt.Println(err.time.Format("Jan _2 15:04:05.00"), err.error) + } + createFile("dd.txt") +} + +type errorOutOfRange struct { + time time.Time + error interface{} +} + +func NewError(s interface{}) *errorOutOfRange { + t := time.Now() + return &errorOutOfRange{ + time: t, + error: s, + } +} + +func thisIsPanic() (err *errorOutOfRange) { + defer func() { + if p := recover(); p != nil { + err = NewError(p) + } + }() + + a := []int{1, 2, 3} + fmt.Println(a[10]) + return nil +} + +func createFile(name string) { + file, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0755) + if err != nil { + fmt.Println(err) + } + _, err = file.Write([]byte("Hello")) + if err != nil { + fmt.Println(err) + } + defer file.Close() + +}