Skip to content

Commit b7b3028

Browse files
committed
Refactoring with interfaces
1 parent e3e84b3 commit b7b3028

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

07-image-abstraction/main.go

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package main
23

34
import (
@@ -8,34 +9,55 @@ import (
89
"image/jpeg"
910
)
1011

11-
func main() {
12-
// call to load the iages
13-
img := loadImage("../images/78771293.jpg")
14-
15-
// loading all the data for images
16-
r,g,b,a := img.At(0,0).RGBA()
17-
18-
// print the informaton
19-
fmt.Printf("%d %d %d %d \n", r,g,b,a)
12+
type loadimage interface {
13+
LoadImage() image.Image
14+
}
2015

16+
type imagen struct {
17+
filename string
2118
}
2219

23-
func loadImage(filename string) image.Image {
24-
25-
// get the files and return and images
26-
f, err := os.Open(filename)
20+
func (fil imagen) LoadImage() image.Image {
21+
22+
fmt.Println("loading interfaces")
23+
f, err := os.Open(fil.filename)
2724

2825
if err != nil {
2926
log.Fatal(err)
3027
}
3128

3229
defer f.Close()
3330

34-
img, err := jpeg.Decode(f)
31+
// get the information of the file
32+
fi, _ := f.Stat()
33+
34+
fmt.Println("File name: \t\t", fi.Name())
35+
fmt.Println("File Size: \t\t", fi.Size())
36+
fmt.Println("File Mode: \t\t", fi.Mode())
37+
fmt.Println("File ModTime: \t\t", fi.ModTime())
38+
fmt.Println("File Is Directory: \t\t", fi.IsDir())
39+
fmt.Println("File Sys: \t\t", fi.Sys())
40+
41+
imgn, err := jpeg.Decode(f)
3542

3643
if err != nil {
3744
log.Fatal(err)
3845
}
3946

40-
return img
41-
}
47+
return imgn
48+
}
49+
50+
func main() {
51+
52+
// call to load the iages
53+
img := imagen{ filename: "../images/78771293.jpg" }
54+
55+
// loading all the data for images
56+
r,g,b,a := img.LoadImage().At(0,0).RGBA()
57+
58+
// print the informaton
59+
fmt.Printf("%d %d %d %d \n", r,g,b,a)
60+
61+
}
62+
63+

0 commit comments

Comments
 (0)