Interfaces assignment one

This commit is contained in:
Johannes Rothe 2022-02-13 22:43:45 +01:00
parent d1a26a5d92
commit a00a517606
3 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,3 @@
module interfaces_assignment1
go 1.17

View File

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

View File

@ -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
}