update: directory structure
This commit is contained in:
111
advfuncs/08-png-detector-with-panic/main.go
Normal file
111
advfuncs/08-png-detector-with-panic/main.go
Normal file
@ -0,0 +1,111 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
/*
|
||||
// ~~~ THE CLASSIC WAY ~~~
|
||||
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
|
||||
|
||||
// really really exceptional problem occurs
|
||||
// mostly due to a programmer
|
||||
|
||||
// panic("PANIC PANIC")
|
||||
*/
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
list("png", files)
|
||||
|
||||
// fmt.Println("catch me if you can!")
|
||||
}
|
||||
|
||||
func list(format string, files []string) {
|
||||
valids := detect(format, files)
|
||||
|
||||
fmt.Printf("Correct Files:\n")
|
||||
for _, valid := range valids {
|
||||
fmt.Printf(" + %s\n", valid)
|
||||
}
|
||||
}
|
||||
|
||||
func detect(format string, filenames []string) (valids []string) {
|
||||
header := headerOf(format)
|
||||
|
||||
buf := make([]byte, len(header))
|
||||
|
||||
for _, filename := range filenames {
|
||||
if read(filename, buf) != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if bytes.Equal([]byte(header), buf) {
|
||||
valids = append(valids, filename)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func headerOf(format string) string {
|
||||
switch format {
|
||||
case "png":
|
||||
return "\x89PNG\r\n\x1a\n"
|
||||
case "jpg":
|
||||
return "\xff\xd8\xff"
|
||||
}
|
||||
panic("unknown format: " + format)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
BIN
advfuncs/08-png-detector-with-panic/pngs/cups-jpg.png
Executable file
BIN
advfuncs/08-png-detector-with-panic/pngs/cups-jpg.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 4.1 KiB |
0
advfuncs/08-png-detector-with-panic/pngs/empty.png
Normal file
0
advfuncs/08-png-detector-with-panic/pngs/empty.png
Normal file
BIN
advfuncs/08-png-detector-with-panic/pngs/forest-jpg.png
Executable file
BIN
advfuncs/08-png-detector-with-panic/pngs/forest-jpg.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
BIN
advfuncs/08-png-detector-with-panic/pngs/golden.png
Normal file
BIN
advfuncs/08-png-detector-with-panic/pngs/golden.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
@ -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
|
BIN
advfuncs/08-png-detector-with-panic/pngs/work.png
Normal file
BIN
advfuncs/08-png-detector-with-panic/pngs/work.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
Reference in New Issue
Block a user