mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:34:15 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -1,65 +1,66 @@
|
||||
//multi_interfaces_poly.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type TopologicalGenus interface {
|
||||
Rank() int
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
func (sq *Square) Rank() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
func (r Rectangle) Rank() int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := Rectangle{5, 3} // Area() of Rectangle needs a value
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
shapes := []Shaper{r, q}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
for n, _ := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
topgen := []TopologicalGenus{r, q}
|
||||
fmt.Println("Looping through topgen for rank ...")
|
||||
for n, _ := range topgen {
|
||||
fmt.Println("Shape details: ", topgen[n])
|
||||
fmt.Println("Topological Genus of this shape is: ", topgen[n].Rank())
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
Looping through shapes for area ...
|
||||
Shape details: {5 3}
|
||||
Area of this shape is: 15
|
||||
Shape details: &{5}
|
||||
Area of this shape is: 25
|
||||
Looping through topgen for rank ...
|
||||
Shape details: {5 3}
|
||||
Topological Genus of this shape is: 2
|
||||
Shape details: &{5}
|
||||
Topological Genus of this shape is: 1
|
||||
*/
|
||||
//multi_interfaces_poly.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type TopologicalGenus interface {
|
||||
Rank() int
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
func (sq *Square) Rank() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
func (r Rectangle) Rank() int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := Rectangle{5, 3} // Area() of Rectangle needs a value
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
shapes := []Shaper{r, q}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
for n := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
topgen := []TopologicalGenus{r, q}
|
||||
fmt.Println("Looping through topgen for rank ...")
|
||||
for n := range topgen {
|
||||
fmt.Println("Shape details: ", topgen[n])
|
||||
fmt.Println("Topological Genus of this shape is: ", topgen[n].Rank())
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Looping through shapes for area ...
|
||||
Shape details: {5 3}
|
||||
Area of this shape is: 15
|
||||
Shape details: &{5}
|
||||
Area of this shape is: 25
|
||||
Looping through topgen for rank ...
|
||||
Shape details: {5 3}
|
||||
Topological Genus of this shape is: 2
|
||||
Shape details: &{5}
|
||||
Topological Genus of this shape is: 1
|
||||
*/
|
||||
|
Reference in New Issue
Block a user