Add structs
This commit is contained in:
parent
134924afca
commit
867a7687e7
9
main.go
Normal file
9
main.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
// import "git.sangeeth.dev/gobyexample/runes"
|
||||
import "git.sangeeth.dev/gobyexample/structs"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
structs.Structs()
|
||||
}
|
26
runes/runes.go
Normal file
26
runes/runes.go
Normal file
@ -0,0 +1,26 @@
|
||||
package runes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func Runes() {
|
||||
s := "Hello, world!"
|
||||
fmt.Printf("%s has %d runes\n", s, utf8.RuneCountInString(s))
|
||||
|
||||
s = "നമസ്കാരം"
|
||||
fmt.Printf("%s has %d runes\n", s, utf8.RuneCountInString(s))
|
||||
|
||||
fmt.Printf("Iterating through %s:\n", s)
|
||||
for i, val := range s {
|
||||
fmt.Printf("%2d = %#U\n", i, val)
|
||||
}
|
||||
|
||||
fmt.Printf("Decoding %s:\n", s)
|
||||
for i, w := 0, 0; i < len(s); i += w {
|
||||
value, width := utf8.DecodeRuneInString(s[i:])
|
||||
fmt.Printf("Decoded rune %#U of size %d\n", value, width)
|
||||
w = width
|
||||
}
|
||||
}
|
90
structs/structs.go
Normal file
90
structs/structs.go
Normal file
@ -0,0 +1,90 @@
|
||||
package structs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type ShapeMetadata struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
|
||||
type Shape interface {
|
||||
Area() float64
|
||||
Perimeter() float64
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
ShapeMetadata
|
||||
Size float64
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
ShapeMetadata
|
||||
Length, Breadth float64
|
||||
}
|
||||
|
||||
type Triangle struct {
|
||||
ShapeMetadata
|
||||
A, B, C float64
|
||||
}
|
||||
|
||||
func (s *Square) Area() float64 {
|
||||
return s.Size * s.Size
|
||||
}
|
||||
|
||||
func (s *Square) Perimeter() float64 {
|
||||
return 4 * s.Size
|
||||
}
|
||||
|
||||
func (r *Rectangle) Area() float64 {
|
||||
return r.Length * r.Breadth
|
||||
}
|
||||
|
||||
func (r *Rectangle) Perimeter() float64 {
|
||||
return 2 * (r.Length + r.Breadth)
|
||||
}
|
||||
|
||||
func (t *Triangle) Area() float64 {
|
||||
s := (t.A + t.B + t.C) / 2
|
||||
area := math.Sqrt(s * (s - t.A) * (s - t.B) * (s - t.C))
|
||||
return area
|
||||
}
|
||||
|
||||
func (t *Triangle) Perimeter() float64 {
|
||||
return t.A + t.B + t.C
|
||||
}
|
||||
|
||||
func Structs() {
|
||||
sq := Square{
|
||||
ShapeMetadata: ShapeMetadata{
|
||||
Id: 0,
|
||||
Name: "mysquare",
|
||||
},
|
||||
Size: 10.0,
|
||||
}
|
||||
|
||||
rect := Rectangle{
|
||||
ShapeMetadata: ShapeMetadata{
|
||||
Id: 0,
|
||||
Name: "myrectangle",
|
||||
},
|
||||
Length: 10,
|
||||
Breadth: 5,
|
||||
}
|
||||
|
||||
tri := Triangle{
|
||||
ShapeMetadata: ShapeMetadata{
|
||||
Id: 0,
|
||||
Name: "mytriangle",
|
||||
},
|
||||
A: 2,
|
||||
B: 2,
|
||||
C: 2,
|
||||
}
|
||||
|
||||
fmt.Printf("Triangle: Area = %.2f Perimeter = %.2f\n", tri.Area(), tri.Perimeter())
|
||||
fmt.Printf("Rectangle: Area = %.2f Perimeter = %.2f\n", rect.Area(), rect.Perimeter())
|
||||
fmt.Printf("Square: Area = %.2f Perimeter = %.2f\n", sq.Area(), sq.Perimeter())
|
||||
}
|
Loading…
Reference in New Issue
Block a user