Interfaces assignment one
This commit is contained in:
parent
d1a26a5d92
commit
a00a517606
3
interfaces_assignment1/go.mod
Normal file
3
interfaces_assignment1/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module interfaces_assignment1
|
||||
|
||||
go 1.17
|
32
interfaces_assignment1/main.go
Normal file
32
interfaces_assignment1/main.go
Normal 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())
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user