diff --git a/interfaces_assignment1/go.mod b/interfaces_assignment1/go.mod new file mode 100644 index 0000000..aff8586 --- /dev/null +++ b/interfaces_assignment1/go.mod @@ -0,0 +1,3 @@ +module interfaces_assignment1 + +go 1.17 diff --git a/interfaces_assignment1/main.go b/interfaces_assignment1/main.go new file mode 100644 index 0000000..3918cb0 --- /dev/null +++ b/interfaces_assignment1/main.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +type triangle struct { + height float64 + base float64 +} +type square struct { + sideLength float64 +} + +type shape interface { + getArea() float64 +} + +func main() { + t := square{sideLength: 0.5} + printArea(t) +} + +func (t triangle) getArea() float64 { + return 0.5 * t.base * t.height +} + +func (s square) getArea() float64 { + return s.sideLength * s.sideLength +} + +func printArea(s shape) { + fmt.Println(s.getArea()) +} diff --git a/interfaces_http/main.go b/interfaces_http/main.go index 4f34616..73d4f11 100644 --- a/interfaces_http/main.go +++ b/interfaces_http/main.go @@ -2,15 +2,27 @@ package main import ( "fmt" + "io" "net/http" "os" ) +type logWriter struct{} + func main() { resp, err := http.Get("https://google.com") if err != nil { fmt.Println("Error!") os.Exit(1) } - fmt.Println(resp) + // bs := make([]byte, 99999) // necessary because Read() only fills a fix amount + // resp.Body.Read(bs) + // fmt.Println(string(bs)) + lw := logWriter{} + io.Copy(lw, resp.Body) +} + +func (l logWriter) Write(bs []byte) (int, error) { + fmt.Println(string(bs)) + return len(bs), nil }