diff --git a/27-functions-advanced/07-deferred-funcs/main.go b/27-functions-advanced/07-deferred-funcs/main.go new file mode 100644 index 0000000..2459d9b --- /dev/null +++ b/27-functions-advanced/07-deferred-funcs/main.go @@ -0,0 +1,68 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "fmt" + "time" +) + +func main() { + // single() + // stacked() + findTheMeaning() +} + +func findTheMeaningNoDefer() { + start := time.Now() + fmt.Printf("%s starts...\n", "findTheMeaning") + + // do some heavy calculation... + time.Sleep(time.Second * 2) + + fmt.Printf("%s took %v\n", "findTheMeaning", time.Since(start)) +} + +func findTheMeaning() { + defer measure("findTheMeaning")() + + // do some heavy calculation + time.Sleep(time.Second * 2) +} + +func measure(name string) func() { + start := time.Now() + fmt.Printf("%s starts...\n", name) + + return func() { + fmt.Printf("%s took %v\n", name, time.Since(start)) + } +} + +func stacked() { + for count := 1; count <= 5; count++ { + defer fmt.Println(count) + } + + fmt.Println("the stacked func returns") +} + +func single() { + var count int + + // defer func() { + // fmt.Println(count) + // }() + defer fmt.Println(count) + + count++ + // fmt.Println(count) + + // the defer runs here + // fmt.Println(count) +} diff --git a/27-functions-advanced/08-panic-recover-todo/TODO.md b/27-functions-advanced/08-panic-recover-todo/TODO.md deleted file mode 100644 index f87f5c1..0000000 --- a/27-functions-advanced/08-panic-recover-todo/TODO.md +++ /dev/null @@ -1 +0,0 @@ -# TODO \ No newline at end of file diff --git a/27-functions-advanced/08-png-detector/main.go b/27-functions-advanced/08-png-detector/main.go new file mode 100644 index 0000000..b2ceafb --- /dev/null +++ b/27-functions-advanced/08-png-detector/main.go @@ -0,0 +1,165 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "bytes" + "fmt" + "io" + "os" +) + +/* + // Most other languages do this: + try { + // open a file + // throws an exception + } catch (ExceptionType name) { + // handle the error + } finally { + // close the file + } + + // Go way: + file, err := // open the file + if err != nil { + // handle the error + } + // close the file +*/ + +func main() { + files := []string{ + "pngs/cups-jpg.png", + "pngs/forest-jpg.png", + "pngs/golden.png", + "pngs/work.png", + "pngs/shakespeare-text.png", + "pngs/empty.png", + } + + pngs := detectPNG(files) + + fmt.Printf("Correct PNG Files:\n") + for _, png := range pngs { + fmt.Printf(" + %s\n", png) + } +} + +func detectPNG(filenames []string) (pngs []string) { + const pngHeader = "\x89PNG\r\n\x1a\n" + + buf := make([]byte, len(pngHeader)) + + for _, filename := range filenames { + if read(filename, buf) != nil { + continue + } + + if bytes.Equal([]byte(pngHeader), buf) { + pngs = append(pngs, filename) + } + } + return +} + +// read reads len(buf) bytes to buf from a file +func read(filename string, buf []byte) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + fi, err := file.Stat() + if err != nil { + return err + } + + if fi.Size() <= int64(len(buf)) { + return fmt.Errorf("file size < len(buf)") + } + + _, err = io.ReadFull(file, buf) + return err +} + +func detectPNGUnsafeAndVerbose(filenames []string) (pngs []string) { + const pngHeader = "\x89PNG\r\n\x1a\n" + + buf := make([]byte, len(pngHeader)) + + for _, filename := range filenames { + file, err := os.Open(filename) + if err != nil { + continue + } + + fi, err := file.Stat() + if err != nil { + file.Close() + continue + } + + if fi.Size() <= int64(len(pngHeader)) { + file.Close() + continue + } + + _, err = io.ReadFull(file, buf) + file.Close() + if err != nil { + continue + } + + if bytes.Equal([]byte(pngHeader), buf) { + pngs = append(pngs, filename) + } + } + return +} + +// defers don't run when the loop block ends +func detectPNGWrong(filenames []string) (pngs []string) { + const pngHeader = "\x89PNG\r\n\x1a\n" + + buf := make([]byte, len(pngHeader)) + + for _, filename := range filenames { + fmt.Printf("processing: %s\n", filename) + + file, err := os.Open(filename) + if err != nil { + continue + } + + defer func() { + fmt.Printf("closes : %s\n", filename) + file.Close() + }() + + fi, err := file.Stat() + if err != nil { + continue + } + + if fi.Size() <= int64(len(pngHeader)) { + continue + } + + _, err = io.ReadFull(file, buf) + if err != nil { + continue + } + + if bytes.Equal([]byte(pngHeader), buf) { + pngs = append(pngs, filename) + } + } + return +} diff --git a/27-functions-advanced/08-png-detector/pngs/cups-jpg.png b/27-functions-advanced/08-png-detector/pngs/cups-jpg.png new file mode 100755 index 0000000..7c0542d Binary files /dev/null and b/27-functions-advanced/08-png-detector/pngs/cups-jpg.png differ diff --git a/27-functions-advanced/08-png-detector/pngs/empty.png b/27-functions-advanced/08-png-detector/pngs/empty.png new file mode 100644 index 0000000..e69de29 diff --git a/27-functions-advanced/08-png-detector/pngs/forest-jpg.png b/27-functions-advanced/08-png-detector/pngs/forest-jpg.png new file mode 100755 index 0000000..2b25dcf Binary files /dev/null and b/27-functions-advanced/08-png-detector/pngs/forest-jpg.png differ diff --git a/27-functions-advanced/08-png-detector/pngs/golden.png b/27-functions-advanced/08-png-detector/pngs/golden.png new file mode 100644 index 0000000..00827bd Binary files /dev/null and b/27-functions-advanced/08-png-detector/pngs/golden.png differ diff --git a/27-functions-advanced/08-png-detector/pngs/shakespeare-text.png b/27-functions-advanced/08-png-detector/pngs/shakespeare-text.png new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/27-functions-advanced/08-png-detector/pngs/shakespeare-text.png @@ -0,0 +1,12 @@ +come night come romeo come thou day in night +for thou wilt lie upon the wings of night +whiter than new snow on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he shall die +take him and cut him out in little stars +and he will make the face of heaven so fine +that all the world will be in love with night +and pay no worship to the garish sun +oh i have bought the mansion of love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/27-functions-advanced/08-png-detector/pngs/work.png b/27-functions-advanced/08-png-detector/pngs/work.png new file mode 100644 index 0000000..75787b2 Binary files /dev/null and b/27-functions-advanced/08-png-detector/pngs/work.png differ diff --git a/27-functions-advanced/07-deferred-funcs-todo/TODO.md b/27-functions-advanced/09-panic-recover-todo/TODO.md similarity index 100% rename from 27-functions-advanced/07-deferred-funcs-todo/TODO.md rename to 27-functions-advanced/09-panic-recover-todo/TODO.md