Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go/study/base/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tecyokomichi/WebAppLearning/go/study/base

go 1.16
24 changes: 24 additions & 0 deletions go/study/base/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "fmt"

func main() {
p1 := &Point{Px: 100, Py: 50}
fmt.Printf("%#v\n", p1)

c1 := NewCircle(p1, 30)
fmt.Printf("%#v\n", c1)
fmt.Printf("面積: %#v\n", c1.Area())
c1.Expand(10)
fmt.Printf("%#v\n", c1)

r1 := &Rect{Point: p1, width: 20, length: 10}
fmt.Printf("面積: %#v\n", r1.Area())
r2 := &Rect{Point: p1, width: 30, length: 20}
r3 := &Rect{Point: p1, width: 40, length: 30}
r4 := &Rect{Point: p1, width: 50, length: 40}
r5 := &Rect{Point: p1, width: 10, length: 5}
fmt.Printf("%#v\n", r1)
fmt.Printf("%#v\n", RecList([]*Rect{r1, r2, r3, r4, r5}).Biggest())
fmt.Printf("%#v\n", Shapes([]Shape{c1, r1, r2, r3, r4, r5}).Biggest())
}
42 changes: 42 additions & 0 deletions go/study/base/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import "testing"

func TestNewCircle(t *testing.T) {
p := &Point{Px: 100, Py: 200}
c := NewCircle(p, 300)
if c.radius != 300 {
t.FailNow()
}
}

func TestExpandCircle(t *testing.T) {
p := &Point{Px: 100, Py: 200}
c := NewCircle(p, 300)
c.Expand(400)
if c.radius != 700 {
t.FailNow()
}
}

func TestArea(t *testing.T) {
p := &Point{Px: 100, Py: 200}
r := &Rect{Point: p, width: 300, length: 400}
if r.Area() != 120000 {
t.FailNow()
}
}

func TestBiggest(t *testing.T) {
p := &Point{Px: 100, Py: 200}
c := NewCircle(p, 300)
r1 := &Rect{Point: p, width: 300, length: 400}
r2 := &Rect{Point: p, width: 30, length: 40}
r3 := &Rect{Point: p, width: 3, length: 4}
if RecList([]*Rect{r1, r2, r3}).Biggest().Area() != 120000 {
t.FailNow()
}
if Shapes([]Shape{c, r1, r2, r3}).Biggest().Area() < 282743 || Shapes([]Shape{c, r1, r2, r3}).Biggest().Area() > 282744 {
t.FailNow()
}
}
61 changes: 61 additions & 0 deletions go/study/base/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import "math"

type Point struct {
Px, Py int
}

type Shape interface {
Area() float64
}

type Circle struct {
radius int
Point *Point
}

type Rect struct {
width, length int
Point *Point
}

func NewCircle(p *Point, r int) *Circle {
return &Circle{Point: p, radius: r}
}

func (c *Circle) Expand(dr int){
c.radius += dr
}

func (c *Circle) Area() float64 {
return float64(c.radius * c.radius) * math.Pi
}

func (r *Rect) Area() float64 {
return float64(r.width * r.length)
}

type RecList []*Rect

func (rl RecList) Biggest() *Rect {
var b *Rect
for _, r := range rl {
if b == nil || r.Area() > b.Area(){
b = r
}
}
return b
}

type Shapes []Shape

func (sha Shapes) Biggest() Shape {
var b Shape
for _, r := range sha {
if b == nil || r.Area() > b.Area(){
b = r
}
}
return b
}