Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lesson_1 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion lesson1/example1/main.go
Original file line number Diff line number Diff line change
@@ -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{}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не круто. Лучше не interface{} а error

}

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Про задание с созданием файлов поговорим на уроке

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()

}