mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
fix: coding style and file format for chapter 11, 12, 13, 14 and 15.
This commit is contained in:
@@ -1,66 +1,66 @@
|
||||
package float64
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Sorter interface {
|
||||
Len() int
|
||||
Less(i, j int) bool
|
||||
Swap(i, j int)
|
||||
}
|
||||
|
||||
func Sort(data Sorter) {
|
||||
for pass := 1; pass < data.Len(); pass++ {
|
||||
for i := 0; i < data.Len()-pass; i++ {
|
||||
if data.Less(i+1, i) {
|
||||
data.Swap(i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsSorted(data Sorter) bool {
|
||||
n := data.Len()
|
||||
for i := n - 1; i > 0; i-- {
|
||||
if data.Less(i, i-1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type Float64Array []float64
|
||||
|
||||
func (p Float64Array) Len() int { return len(p) }
|
||||
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
func NewFloat64Array() Float64Array {
|
||||
return make([]float64, 25)
|
||||
}
|
||||
|
||||
func (p Float64Array) Fill(n int) {
|
||||
rand.Seed(int64(time.Now().Nanosecond()))
|
||||
for i := 0; i < n; i++ {
|
||||
p[i] = 100 * (rand.Float64())
|
||||
}
|
||||
}
|
||||
|
||||
func (p Float64Array) List() string {
|
||||
s := "{ "
|
||||
for i := 0; i < p.Len(); i++ {
|
||||
if p[i] == 0 {
|
||||
continue
|
||||
}
|
||||
s += fmt.Sprintf("%3.1f ", p[i])
|
||||
}
|
||||
s += " }"
|
||||
return s
|
||||
}
|
||||
|
||||
func (p Float64Array) String() string {
|
||||
return p.List()
|
||||
}
|
||||
package float64
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Sorter interface {
|
||||
Len() int
|
||||
Less(i, j int) bool
|
||||
Swap(i, j int)
|
||||
}
|
||||
|
||||
func Sort(data Sorter) {
|
||||
for pass := 1; pass < data.Len(); pass++ {
|
||||
for i := 0; i < data.Len()-pass; i++ {
|
||||
if data.Less(i+1, i) {
|
||||
data.Swap(i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsSorted(data Sorter) bool {
|
||||
n := data.Len()
|
||||
for i := n - 1; i > 0; i-- {
|
||||
if data.Less(i, i-1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type Float64Array []float64
|
||||
|
||||
func (p Float64Array) Len() int { return len(p) }
|
||||
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
func NewFloat64Array() Float64Array {
|
||||
return make([]float64, 25)
|
||||
}
|
||||
|
||||
func (p Float64Array) Fill(n int) {
|
||||
rand.Seed(int64(time.Now().Nanosecond()))
|
||||
for i := 0; i < n; i++ {
|
||||
p[i] = 100 * (rand.Float64())
|
||||
}
|
||||
}
|
||||
|
||||
func (p Float64Array) List() string {
|
||||
s := "{ "
|
||||
for i := 0; i < p.Len(); i++ {
|
||||
if p[i] == 0 {
|
||||
continue
|
||||
}
|
||||
s += fmt.Sprintf("%3.1f ", p[i])
|
||||
}
|
||||
s += " }"
|
||||
return s
|
||||
}
|
||||
|
||||
func (p Float64Array) String() string {
|
||||
return p.List()
|
||||
}
|
||||
|
@@ -1,25 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./float64"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f1 := float64.NewFloat64Array()
|
||||
f1.Fill(10)
|
||||
fmt.Printf("Before sorting %s\n", f1)
|
||||
float64.Sort(f1)
|
||||
fmt.Printf("After sorting %s\n", f1)
|
||||
if float64.IsSorted(f1) {
|
||||
fmt.Println("The float64 array is sorted!")
|
||||
} else {
|
||||
fmt.Println("The float64 array is NOT sorted!")
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Before sorting { 55.0 82.3 36.4 66.6 25.3 82.7 47.4 21.5 4.6 81.6 }
|
||||
After sorting { 4.6 21.5 25.3 36.4 47.4 55.0 66.6 81.6 82.3 82.7 }
|
||||
The float64 array is sorted!
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"./float64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f1 := float64.NewFloat64Array()
|
||||
f1.Fill(10)
|
||||
fmt.Printf("Before sorting %s\n", f1)
|
||||
float64.Sort(f1)
|
||||
fmt.Printf("After sorting %s\n", f1)
|
||||
if float64.IsSorted(f1) {
|
||||
fmt.Println("The float64 array is sorted!")
|
||||
} else {
|
||||
fmt.Println("The float64 array is NOT sorted!")
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Before sorting { 55.0 82.3 36.4 66.6 25.3 82.7 47.4 21.5 4.6 81.6 }
|
||||
After sorting { 4.6 21.5 25.3 36.4 47.4 55.0 66.6 81.6 82.3 82.7 }
|
||||
The float64 array is sorted!
|
||||
*/
|
||||
|
@@ -1,46 +1,46 @@
|
||||
// interface_nil.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Any interface {}
|
||||
type Anything struct {}
|
||||
|
||||
func main() {
|
||||
any := getAny()
|
||||
if any == nil {
|
||||
fmt.Println("any is nil")
|
||||
} else {
|
||||
fmt.Println("any is not nil")
|
||||
}
|
||||
/*
|
||||
// to get the inner value:
|
||||
anything := any.(*Anything)
|
||||
if anything == nil {
|
||||
fmt.Println("anything is nil")
|
||||
} else {
|
||||
fmt.Println("anything is not nil")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func getAny() Any {
|
||||
return getAnything()
|
||||
}
|
||||
|
||||
func getAnything() *Anything {
|
||||
return nil
|
||||
}
|
||||
|
||||
/* Output:
|
||||
any is not nil
|
||||
WHY?
|
||||
you would perhaps expect: any is nil,because getAnything() returns that
|
||||
BUT:
|
||||
the interface value any is storing a value, so it is not nil.
|
||||
It just so happens that the particular value it is storing is a nil pointer.
|
||||
The any variable has a type, so it's not a nil interface,
|
||||
rather an interface variable with type Any and concrete value (*Anything)(nil).
|
||||
To get the inner value of any, use: anything := any.(*Anything)
|
||||
now anything contains nil !
|
||||
*/
|
||||
// interface_nil.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Any interface{}
|
||||
type Anything struct{}
|
||||
|
||||
func main() {
|
||||
any := getAny()
|
||||
if any == nil {
|
||||
fmt.Println("any is nil")
|
||||
} else {
|
||||
fmt.Println("any is not nil")
|
||||
}
|
||||
/*
|
||||
// to get the inner value:
|
||||
anything := any.(*Anything)
|
||||
if anything == nil {
|
||||
fmt.Println("anything is nil")
|
||||
} else {
|
||||
fmt.Println("anything is not nil")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func getAny() Any {
|
||||
return getAnything()
|
||||
}
|
||||
|
||||
func getAnything() *Anything {
|
||||
return nil
|
||||
}
|
||||
|
||||
/* Output:
|
||||
any is not nil
|
||||
WHY?
|
||||
you would perhaps expect: any is nil,because getAnything() returns that
|
||||
BUT:
|
||||
the interface value any is storing a value, so it is not nil.
|
||||
It just so happens that the particular value it is storing is a nil pointer.
|
||||
The any variable has a type, so it's not a nil interface,
|
||||
rather an interface variable with type Any and concrete value (*Anything)(nil).
|
||||
To get the inner value of any, use: anything := any.(*Anything)
|
||||
now anything contains nil !
|
||||
*/
|
||||
|
@@ -1,68 +1,69 @@
|
||||
// interface_poly3.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type Shape struct {}
|
||||
func (sh Shape) Area() float32 {
|
||||
return -1 // the shape is indetermined, so we return something impossible
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (r *Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (c *Circle) Area() float32 {
|
||||
return math.Pi * c.radius * c.radius
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := Shape{}
|
||||
r := &Rectangle{5, 3, s} // Area() of Rectangle needs a value
|
||||
q := &Square{5, s} // Area() of Square needs a pointer
|
||||
c := &Circle{2.5, s}
|
||||
shapes := []Shaper{r, q, c, s}
|
||||
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())
|
||||
}
|
||||
}
|
||||
/* 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
|
||||
Shape details: &{2.5}
|
||||
Area of this shape is: 19.634954
|
||||
Shape details: {}
|
||||
Area of this shape is: -1
|
||||
*/
|
||||
|
||||
// interface_poly3.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type Shape struct{}
|
||||
|
||||
func (sh Shape) Area() float32 {
|
||||
return -1 // the shape is indetermined, so we return something impossible
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (r *Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float32
|
||||
Shape
|
||||
}
|
||||
|
||||
func (c *Circle) Area() float32 {
|
||||
return math.Pi * c.radius * c.radius
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := Shape{}
|
||||
r := &Rectangle{5, 3, s} // Area() of Rectangle needs a value
|
||||
q := &Square{5, s} // Area() of Square needs a pointer
|
||||
c := &Circle{2.5, s}
|
||||
shapes := []Shaper{r, q, c, s}
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
/* 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
|
||||
Shape details: &{2.5}
|
||||
Area of this shape is: 19.634954
|
||||
Shape details: {}
|
||||
Area of this shape is: -1
|
||||
*/
|
||||
|
@@ -1,51 +1,52 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
type Triangle struct {
|
||||
base float32
|
||||
height float32
|
||||
}
|
||||
|
||||
type AreaInterface interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type PeriInterface interface {
|
||||
Perimeter() float32
|
||||
}
|
||||
func main() {
|
||||
var areaIntf AreaInterface
|
||||
var periIntf PeriInterface
|
||||
|
||||
sq1 := new(Square)
|
||||
sq1.side = 5
|
||||
tr1 := new(Triangle)
|
||||
tr1.base = 3
|
||||
tr1.height = 5
|
||||
|
||||
areaIntf = sq1
|
||||
fmt.Printf("The square has area: %f\n", areaIntf.Area())
|
||||
|
||||
periIntf = sq1
|
||||
fmt.Printf("The square has perimeter: %f\n", periIntf.Perimeter())
|
||||
|
||||
areaIntf = tr1
|
||||
fmt.Printf("The triangle has area: %f\n", areaIntf.Area())
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
func (sq *Square) Perimeter() float32 {
|
||||
return 4 * sq.side
|
||||
}
|
||||
|
||||
func (tr *Triangle) Area() float32 {
|
||||
return 0.5 * tr.base*tr.height
|
||||
}
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
type Triangle struct {
|
||||
base float32
|
||||
height float32
|
||||
}
|
||||
|
||||
type AreaInterface interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type PeriInterface interface {
|
||||
Perimeter() float32
|
||||
}
|
||||
|
||||
func main() {
|
||||
var areaIntf AreaInterface
|
||||
var periIntf PeriInterface
|
||||
|
||||
sq1 := new(Square)
|
||||
sq1.side = 5
|
||||
tr1 := new(Triangle)
|
||||
tr1.base = 3
|
||||
tr1.height = 5
|
||||
|
||||
areaIntf = sq1
|
||||
fmt.Printf("The square has area: %f\n", areaIntf.Area())
|
||||
|
||||
periIntf = sq1
|
||||
fmt.Printf("The square has perimeter: %f\n", periIntf.Perimeter())
|
||||
|
||||
areaIntf = tr1
|
||||
fmt.Printf("The triangle has area: %f\n", areaIntf.Area())
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
func (sq *Square) Perimeter() float32 {
|
||||
return 4 * sq.side
|
||||
}
|
||||
|
||||
func (tr *Triangle) Area() float32 {
|
||||
return 0.5 * tr.base * tr.height
|
||||
}
|
||||
|
@@ -1,57 +1,58 @@
|
||||
// interfaces_poly2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float32
|
||||
}
|
||||
|
||||
func (c *Circle) Area() float32 {
|
||||
return math.Pi * c.radius * c.radius
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := Rectangle{5, 3} // Area() of Rectangle needs a value
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
c := &Circle{2.5}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
// shapes := []Shaper{Shaper(r), Shaper(q), Shaper(c)}
|
||||
shapes := []Shaper{r, q, c}
|
||||
for n, _ := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
}
|
||||
/* 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
|
||||
Shape details: &{2.5}
|
||||
Area of this shape is: 19.634954
|
||||
*/
|
||||
// interfaces_poly2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Shaper interface {
|
||||
Area() float32
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
side float32
|
||||
}
|
||||
|
||||
func (sq *Square) Area() float32 {
|
||||
return sq.side * sq.side
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
length, width float32
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() float32 {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float32
|
||||
}
|
||||
|
||||
func (c *Circle) Area() float32 {
|
||||
return math.Pi * c.radius * c.radius
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := Rectangle{5, 3} // Area() of Rectangle needs a value
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
c := &Circle{2.5}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
// shapes := []Shaper{Shaper(r), Shaper(q), Shaper(c)}
|
||||
shapes := []Shaper{r, q, c}
|
||||
for n := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
}
|
||||
|
||||
/* 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
|
||||
Shape details: &{2.5}
|
||||
Area of this shape is: 19.634954
|
||||
*/
|
||||
|
@@ -1,29 +1,30 @@
|
||||
// main_stack.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./stack/stack"
|
||||
)
|
||||
|
||||
var st1 stack.Stack
|
||||
|
||||
func main() {
|
||||
st1.Push("Brown")
|
||||
st1.Push(3.14)
|
||||
st1.Push(100)
|
||||
st1.Push([]string{"Java", "C++", "Python", "C#", "Ruby"})
|
||||
for {
|
||||
item, err := st1.Pop()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(item)
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
[Java C++ Python C# Ruby]
|
||||
100
|
||||
3.14
|
||||
Brown
|
||||
*/
|
||||
// main_stack.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"./stack/stack"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var st1 stack.Stack
|
||||
|
||||
func main() {
|
||||
st1.Push("Brown")
|
||||
st1.Push(3.14)
|
||||
st1.Push(100)
|
||||
st1.Push([]string{"Java", "C++", "Python", "C#", "Ruby"})
|
||||
for {
|
||||
item, err := st1.Pop()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(item)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
[Java C++ Python C# Ruby]
|
||||
100
|
||||
3.14
|
||||
Brown
|
||||
*/
|
||||
|
@@ -1,17 +1,17 @@
|
||||
// main_stack_v2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"./stack/collection"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var s collection.Stack
|
||||
s.Push("world")
|
||||
s.Push("hello, ")
|
||||
for s.Size() > 0 {
|
||||
fmt.Print(s.Pop())
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
// main_stack_v2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"./stack/collection"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var s collection.Stack
|
||||
s.Push("world")
|
||||
s.Push("hello, ")
|
||||
for s.Size() > 0 {
|
||||
fmt.Print(s.Pop())
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
@@ -1,62 +1,63 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type obj interface{}
|
||||
|
||||
func main() {
|
||||
// define a generic lambda function mf:
|
||||
mf := func(i obj) obj {
|
||||
switch i.(type) {
|
||||
case int:
|
||||
return i.(int) * 2
|
||||
case string:
|
||||
return i.(string) + i.(string)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
isl := []obj{0, 1, 2, 3, 4, 5}
|
||||
res1 := mapFunc(mf, isl)
|
||||
for _, v := range res1 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
println()
|
||||
|
||||
ssl := []obj{"0", "1", "2", "3", "4", "5"}
|
||||
res2 := mapFunc(mf, ssl)
|
||||
for _, v := range res2 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func mapFunc(mf func(obj) obj, list []obj) ([]obj) {
|
||||
result := make([]obj, len(list))
|
||||
|
||||
for ix, v := range list {
|
||||
result[ix] = mf(v)
|
||||
}
|
||||
|
||||
// Equivalent:
|
||||
/*
|
||||
for ix := 0; ix<len(list); ix++ {
|
||||
result[ix] = mf(list[ix])
|
||||
}
|
||||
*/
|
||||
return result
|
||||
}
|
||||
/* Output:
|
||||
0
|
||||
2
|
||||
4
|
||||
6
|
||||
8
|
||||
10
|
||||
|
||||
00
|
||||
11
|
||||
22
|
||||
33
|
||||
44
|
||||
55
|
||||
*/
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type obj interface{}
|
||||
|
||||
func main() {
|
||||
// define a generic lambda function mf:
|
||||
mf := func(i obj) obj {
|
||||
switch i.(type) {
|
||||
case int:
|
||||
return i.(int) * 2
|
||||
case string:
|
||||
return i.(string) + i.(string)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
isl := []obj{0, 1, 2, 3, 4, 5}
|
||||
res1 := mapFunc(mf, isl)
|
||||
for _, v := range res1 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
println()
|
||||
|
||||
ssl := []obj{"0", "1", "2", "3", "4", "5"}
|
||||
res2 := mapFunc(mf, ssl)
|
||||
for _, v := range res2 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func mapFunc(mf func(obj) obj, list []obj) []obj {
|
||||
result := make([]obj, len(list))
|
||||
|
||||
for ix, v := range list {
|
||||
result[ix] = mf(v)
|
||||
}
|
||||
|
||||
// Equivalent:
|
||||
/*
|
||||
for ix := 0; ix<len(list); ix++ {
|
||||
result[ix] = mf(list[ix])
|
||||
}
|
||||
*/
|
||||
return result
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0
|
||||
2
|
||||
4
|
||||
6
|
||||
8
|
||||
10
|
||||
|
||||
00
|
||||
11
|
||||
22
|
||||
33
|
||||
44
|
||||
55
|
||||
*/
|
||||
|
@@ -1,59 +1,60 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type obj interface{}
|
||||
|
||||
func main() {
|
||||
// define a generic lambda function mf:
|
||||
mf := func(i obj) obj {
|
||||
switch i.(type) {
|
||||
case int:
|
||||
return i.(int) * 2
|
||||
case string:
|
||||
return i.(string) + i.(string)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
res1 := mapFunc(mf, 0, 1, 2, 3, 4, 5)
|
||||
for _, v := range res1 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
println()
|
||||
res2 := mapFunc(mf, "0", "1", "2", "3", "4", "5")
|
||||
for _, v := range res2 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func mapFunc(mf func(obj) obj, list ...obj) ([]obj) {
|
||||
result := make([]obj, len(list))
|
||||
|
||||
for ix, v := range list {
|
||||
result[ix] = mf(v)
|
||||
}
|
||||
|
||||
// Equivalent:
|
||||
/*
|
||||
for ix := 0; ix<len(list); ix++ {
|
||||
result[ix] = mf(list[ix])
|
||||
}
|
||||
*/
|
||||
return result
|
||||
}
|
||||
/* Output:
|
||||
0
|
||||
2
|
||||
4
|
||||
6
|
||||
8
|
||||
10
|
||||
|
||||
00
|
||||
11
|
||||
22
|
||||
33
|
||||
44
|
||||
55
|
||||
*/
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type obj interface{}
|
||||
|
||||
func main() {
|
||||
// define a generic lambda function mf:
|
||||
mf := func(i obj) obj {
|
||||
switch i.(type) {
|
||||
case int:
|
||||
return i.(int) * 2
|
||||
case string:
|
||||
return i.(string) + i.(string)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
res1 := mapFunc(mf, 0, 1, 2, 3, 4, 5)
|
||||
for _, v := range res1 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
println()
|
||||
res2 := mapFunc(mf, "0", "1", "2", "3", "4", "5")
|
||||
for _, v := range res2 {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func mapFunc(mf func(obj) obj, list ...obj) []obj {
|
||||
result := make([]obj, len(list))
|
||||
|
||||
for ix, v := range list {
|
||||
result[ix] = mf(v)
|
||||
}
|
||||
|
||||
// Equivalent:
|
||||
/*
|
||||
for ix := 0; ix<len(list); ix++ {
|
||||
result[ix] = mf(list[ix])
|
||||
}
|
||||
*/
|
||||
return result
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0
|
||||
2
|
||||
4
|
||||
6
|
||||
8
|
||||
10
|
||||
|
||||
00
|
||||
11
|
||||
22
|
||||
33
|
||||
44
|
||||
55
|
||||
*/
|
||||
|
@@ -1,28 +1,30 @@
|
||||
// min_interface.go
|
||||
package min
|
||||
|
||||
type Miner interface {
|
||||
Len() int
|
||||
ElemIx(ix int) interface{}
|
||||
Less(i, j int) bool
|
||||
}
|
||||
|
||||
func Min(data Miner) interface{} {
|
||||
min := data.ElemIx(0)
|
||||
for i:=1; i < data.Len(); i++ {
|
||||
if data.Less(i, i-1) {
|
||||
min = data.ElemIx(i)
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
type IntArray []int
|
||||
func (p IntArray) Len() int { return len(p) }
|
||||
func (p IntArray) ElemIx(ix int) interface{} { return p[ix] }
|
||||
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
|
||||
type StringArray []string
|
||||
func (p StringArray) Len() int { return len(p) }
|
||||
func (p StringArray) ElemIx(ix int) interface{} { return p[ix] }
|
||||
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
// min_interface.go
|
||||
package min
|
||||
|
||||
type Miner interface {
|
||||
Len() int
|
||||
ElemIx(ix int) interface{}
|
||||
Less(i, j int) bool
|
||||
}
|
||||
|
||||
func Min(data Miner) interface{} {
|
||||
min := data.ElemIx(0)
|
||||
for i := 1; i < data.Len(); i++ {
|
||||
if data.Less(i, i-1) {
|
||||
min = data.ElemIx(i)
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
type IntArray []int
|
||||
|
||||
func (p IntArray) Len() int { return len(p) }
|
||||
func (p IntArray) ElemIx(ix int) interface{} { return p[ix] }
|
||||
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
|
||||
type StringArray []string
|
||||
|
||||
func (p StringArray) Len() int { return len(p) }
|
||||
func (p StringArray) ElemIx(ix int) interface{} { return p[ix] }
|
||||
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
|
@@ -1,31 +1,31 @@
|
||||
// minmain.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./min"
|
||||
)
|
||||
|
||||
func ints() {
|
||||
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
|
||||
a := min.IntArray(data) //conversion to type IntArray
|
||||
m := min.Min(a)
|
||||
fmt.Printf("The minimum of the array is: %v\n", m)
|
||||
}
|
||||
|
||||
func strings() {
|
||||
data := []string{"ddd", "eee", "bbb", "ccc", "aaa"}
|
||||
a := min.StringArray(data)
|
||||
m := min.Min(a)
|
||||
fmt.Printf("The minimum of the array is: %v\n", m)
|
||||
}
|
||||
|
||||
func main() {
|
||||
ints()
|
||||
strings()
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The minimum of the array is: -5467984
|
||||
The minimum of the array is: aaa
|
||||
*/
|
||||
// minmain.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"./min"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ints() {
|
||||
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
|
||||
a := min.IntArray(data) //conversion to type IntArray
|
||||
m := min.Min(a)
|
||||
fmt.Printf("The minimum of the array is: %v\n", m)
|
||||
}
|
||||
|
||||
func strings() {
|
||||
data := []string{"ddd", "eee", "bbb", "ccc", "aaa"}
|
||||
a := min.StringArray(data)
|
||||
m := min.Min(a)
|
||||
fmt.Printf("The minimum of the array is: %v\n", m)
|
||||
}
|
||||
|
||||
func main() {
|
||||
ints()
|
||||
strings()
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The minimum of the array is: -5467984
|
||||
The minimum of the array is: aaa
|
||||
*/
|
||||
|
@@ -1,89 +1,90 @@
|
||||
// float64 is necessary as input to math.Sqrt()
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Magnitude interface {
|
||||
Abs() float64
|
||||
}
|
||||
|
||||
var m Magnitude
|
||||
|
||||
type Point struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
func (p *Point) Scale(s float64) {
|
||||
p.X *= s
|
||||
p.Y *= s
|
||||
}
|
||||
|
||||
func (p *Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
|
||||
type Point3 struct {
|
||||
X, Y, Z float64
|
||||
}
|
||||
|
||||
func (p *Point3) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z))
|
||||
}
|
||||
|
||||
type Polar struct {
|
||||
R, Theta float64
|
||||
}
|
||||
|
||||
func (p Polar) Abs() float64 { return p.R }
|
||||
|
||||
func main() {
|
||||
p1 := new(Point)
|
||||
p1.X = 3
|
||||
p1.Y = 4
|
||||
m = p1 // p1 is type *Point, has method Abs()
|
||||
fmt.Printf("The length of the vector p1 is: %f\n", m.Abs())
|
||||
|
||||
p2:= &Point{4, 5}
|
||||
m = p2
|
||||
fmt.Printf("The length of the vector p2 is: %f\n", m.Abs() )
|
||||
|
||||
p1.Scale(5)
|
||||
m = p1
|
||||
fmt.Printf("The length of the vector p1 after scaling is: %f\n", m.Abs() )
|
||||
fmt.Printf("Point p1 after scaling has the following coordinates: X %f - Y %f\n", p1.X, p1.Y)
|
||||
|
||||
mag := m.Abs()
|
||||
m = &Point3{3, 4, 5}
|
||||
mag += m.Abs()
|
||||
m = Polar{2.0, math.Pi / 2}
|
||||
mag += m.Abs()
|
||||
fmt.Printf("The float64 mag is now: %f", mag )
|
||||
}
|
||||
/* Output:
|
||||
The length of the vector p1 is: 5.000000
|
||||
The length of the vector p2 is: 6.403124
|
||||
The length of the vector p1 after scaling is: 25.000000
|
||||
Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000
|
||||
The float64 mag is now: 34.071068
|
||||
|
||||
-- instead of:
|
||||
func (p *Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
m = p1
|
||||
we can write:
|
||||
func (p Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
m = p1
|
||||
and instead of:
|
||||
func (p Polar) Abs() float64 { return p.R }
|
||||
m = Polar{2.0, math.Pi / 2}
|
||||
we can write:
|
||||
func (p *Polar) Abs() float64 { return p.R }
|
||||
m = &Polar{2.0, math.Pi / 2}
|
||||
with the same output
|
||||
*/
|
||||
// float64 is necessary as input to math.Sqrt()
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Magnitude interface {
|
||||
Abs() float64
|
||||
}
|
||||
|
||||
var m Magnitude
|
||||
|
||||
type Point struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
func (p *Point) Scale(s float64) {
|
||||
p.X *= s
|
||||
p.Y *= s
|
||||
}
|
||||
|
||||
func (p *Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
|
||||
type Point3 struct {
|
||||
X, Y, Z float64
|
||||
}
|
||||
|
||||
func (p *Point3) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z))
|
||||
}
|
||||
|
||||
type Polar struct {
|
||||
R, Theta float64
|
||||
}
|
||||
|
||||
func (p Polar) Abs() float64 { return p.R }
|
||||
|
||||
func main() {
|
||||
p1 := new(Point)
|
||||
p1.X = 3
|
||||
p1.Y = 4
|
||||
m = p1 // p1 is type *Point, has method Abs()
|
||||
fmt.Printf("The length of the vector p1 is: %f\n", m.Abs())
|
||||
|
||||
p2 := &Point{4, 5}
|
||||
m = p2
|
||||
fmt.Printf("The length of the vector p2 is: %f\n", m.Abs())
|
||||
|
||||
p1.Scale(5)
|
||||
m = p1
|
||||
fmt.Printf("The length of the vector p1 after scaling is: %f\n", m.Abs())
|
||||
fmt.Printf("Point p1 after scaling has the following coordinates: X %f - Y %f\n", p1.X, p1.Y)
|
||||
|
||||
mag := m.Abs()
|
||||
m = &Point3{3, 4, 5}
|
||||
mag += m.Abs()
|
||||
m = Polar{2.0, math.Pi / 2}
|
||||
mag += m.Abs()
|
||||
fmt.Printf("The float64 mag is now: %f", mag)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The length of the vector p1 is: 5.000000
|
||||
The length of the vector p2 is: 6.403124
|
||||
The length of the vector p1 after scaling is: 25.000000
|
||||
Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000
|
||||
The float64 mag is now: 34.071068
|
||||
|
||||
-- instead of:
|
||||
func (p *Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
m = p1
|
||||
we can write:
|
||||
func (p Point) Abs() float64 {
|
||||
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
|
||||
}
|
||||
m = p1
|
||||
and instead of:
|
||||
func (p Polar) Abs() float64 { return p.R }
|
||||
m = Polar{2.0, math.Pi / 2}
|
||||
we can write:
|
||||
func (p *Polar) Abs() float64 { return p.R }
|
||||
m = &Polar{2.0, math.Pi / 2}
|
||||
with the same output
|
||||
*/
|
||||
|
@@ -1,43 +1,50 @@
|
||||
// print.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Stringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type Celsius float64
|
||||
|
||||
func (c Celsius) String() string {
|
||||
return strconv.FormatFloat(float64(c),'f', 1, 64) + " °C"
|
||||
}
|
||||
|
||||
type Day int
|
||||
|
||||
var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|
||||
func (day Day) String() string {
|
||||
return dayName[day]
|
||||
}
|
||||
|
||||
func print(args ...interface{}) {
|
||||
for i, arg := range args {
|
||||
if i > 0 {os.Stdout.WriteString(" ")}
|
||||
switch a := arg.(type) { // type switch
|
||||
case Stringer: os.Stdout.WriteString(a.String())
|
||||
case int: os.Stdout.WriteString(strconv.Itoa(a))
|
||||
case string: os.Stdout.WriteString(a)
|
||||
// more types
|
||||
default: os.Stdout.WriteString("???")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C
|
||||
}
|
||||
// Tuesday was 18.4 °C
|
||||
// print.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Stringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type Celsius float64
|
||||
|
||||
func (c Celsius) String() string {
|
||||
return strconv.FormatFloat(float64(c), 'f', 1, 64) + " °C"
|
||||
}
|
||||
|
||||
type Day int
|
||||
|
||||
var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|
||||
func (day Day) String() string {
|
||||
return dayName[day]
|
||||
}
|
||||
|
||||
func print(args ...interface{}) {
|
||||
for i, arg := range args {
|
||||
if i > 0 {
|
||||
os.Stdout.WriteString(" ")
|
||||
}
|
||||
switch a := arg.(type) { // type switch
|
||||
case Stringer:
|
||||
os.Stdout.WriteString(a.String())
|
||||
case int:
|
||||
os.Stdout.WriteString(strconv.Itoa(a))
|
||||
case string:
|
||||
os.Stdout.WriteString(a)
|
||||
// more types
|
||||
default:
|
||||
os.Stdout.WriteString("???")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C
|
||||
}
|
||||
|
||||
// Tuesday was 18.4 °C
|
||||
|
@@ -1,34 +1,35 @@
|
||||
// simple_interface.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Put(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Put(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
it.Put(5)
|
||||
return it.Get()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Simple
|
||||
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
}
|
||||
// Output: 5
|
||||
// simple_interface.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Put(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Put(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
it.Put(5)
|
||||
return it.Get()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Simple
|
||||
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
}
|
||||
|
||||
// Output: 5
|
||||
|
@@ -1,61 +1,62 @@
|
||||
// simple_interface2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Set(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Set(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
type RSimple struct {
|
||||
i int
|
||||
j int
|
||||
}
|
||||
|
||||
func (p *RSimple) Get() int {
|
||||
return p.j
|
||||
}
|
||||
|
||||
func (p *RSimple) Set(u int) {
|
||||
p.j = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
switch it.(type) {
|
||||
case *Simple:
|
||||
it.Set(5)
|
||||
return it.Get()
|
||||
case *RSimple:
|
||||
it.Set(50)
|
||||
return it.Get()
|
||||
default:
|
||||
return 99
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Simple
|
||||
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
var r RSimple
|
||||
fmt.Println(fI(&r))
|
||||
}
|
||||
/* Output:
|
||||
5
|
||||
50
|
||||
*/
|
||||
// simple_interface2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Set(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Set(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
type RSimple struct {
|
||||
i int
|
||||
j int
|
||||
}
|
||||
|
||||
func (p *RSimple) Get() int {
|
||||
return p.j
|
||||
}
|
||||
|
||||
func (p *RSimple) Set(u int) {
|
||||
p.j = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
switch it.(type) {
|
||||
case *Simple:
|
||||
it.Set(5)
|
||||
return it.Get()
|
||||
case *RSimple:
|
||||
it.Set(50)
|
||||
return it.Get()
|
||||
default:
|
||||
return 99
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Simple
|
||||
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
var r RSimple
|
||||
fmt.Println(fI(&r))
|
||||
}
|
||||
|
||||
/* Output:
|
||||
5
|
||||
50
|
||||
*/
|
||||
|
@@ -1,74 +1,75 @@
|
||||
// simple_interface2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Set(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Set(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
type RSimple struct {
|
||||
i int
|
||||
j int
|
||||
}
|
||||
|
||||
func (p *RSimple) Get() int {
|
||||
return p.j
|
||||
}
|
||||
|
||||
func (p *RSimple) Set(u int) {
|
||||
p.j = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
switch it.(type) {
|
||||
case *Simple:
|
||||
it.Set(5)
|
||||
return it.Get()
|
||||
case *RSimple:
|
||||
it.Set(50)
|
||||
return it.Get()
|
||||
default:
|
||||
return 99
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
func gI(any interface{}) int {
|
||||
// return any.(Simpler).Get() // unsafe, runtime panic possible
|
||||
if v, ok := any.(Simpler); ok {
|
||||
return v.Get()
|
||||
}
|
||||
return 0 // default value
|
||||
}
|
||||
/* Output:
|
||||
6
|
||||
60
|
||||
*/
|
||||
|
||||
func main() {
|
||||
var s Simple = Simple{6}
|
||||
fmt.Println(gI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
var r RSimple = RSimple{60,60}
|
||||
fmt.Println(gI(&r))
|
||||
}
|
||||
/* Output:
|
||||
6
|
||||
60
|
||||
*/
|
||||
// simple_interface2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Simpler interface {
|
||||
Get() int
|
||||
Set(int)
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
i int
|
||||
}
|
||||
|
||||
func (p *Simple) Get() int {
|
||||
return p.i
|
||||
}
|
||||
|
||||
func (p *Simple) Set(u int) {
|
||||
p.i = u
|
||||
}
|
||||
|
||||
type RSimple struct {
|
||||
i int
|
||||
j int
|
||||
}
|
||||
|
||||
func (p *RSimple) Get() int {
|
||||
return p.j
|
||||
}
|
||||
|
||||
func (p *RSimple) Set(u int) {
|
||||
p.j = u
|
||||
}
|
||||
|
||||
func fI(it Simpler) int {
|
||||
switch it.(type) {
|
||||
case *Simple:
|
||||
it.Set(5)
|
||||
return it.Get()
|
||||
case *RSimple:
|
||||
it.Set(50)
|
||||
return it.Get()
|
||||
default:
|
||||
return 99
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func gI(any interface{}) int {
|
||||
// return any.(Simpler).Get() // unsafe, runtime panic possible
|
||||
if v, ok := any.(Simpler); ok {
|
||||
return v.Get()
|
||||
}
|
||||
return 0 // default value
|
||||
}
|
||||
|
||||
/* Output:
|
||||
6
|
||||
60
|
||||
*/
|
||||
|
||||
func main() {
|
||||
var s Simple = Simple{6}
|
||||
fmt.Println(gI(&s)) // &s is required because Get() is defined with a receiver type pointer
|
||||
var r RSimple = RSimple{60, 60}
|
||||
fmt.Println(gI(&r))
|
||||
}
|
||||
|
||||
/* Output:
|
||||
6
|
||||
60
|
||||
*/
|
||||
|
@@ -1,60 +1,60 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
package sort
|
||||
|
||||
type Sorter interface {
|
||||
Len() int
|
||||
Less(i, j int) bool
|
||||
Swap(i, j int)
|
||||
}
|
||||
|
||||
/*
|
||||
func Sort(Sorter Interface) {
|
||||
for i := 1; i < data.Len(); i++ {
|
||||
for j := i; j > 0 && data.Less(j, j-1); j-- {
|
||||
data.Swap(j, j-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func Sort(data Sorter) {
|
||||
for pass:=1; pass < data.Len(); pass++ {
|
||||
for i:=0; i < data.Len() - pass; i++ {
|
||||
if data.Less(i+1, i) {
|
||||
data.Swap(i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsSorted(data Sorter) bool {
|
||||
n := data.Len()
|
||||
for i := n - 1; i > 0; i-- {
|
||||
if data.Less(i, i-1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Convenience types for common cases
|
||||
type IntArray []int
|
||||
|
||||
func (p IntArray) Len() int { return len(p) }
|
||||
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
type StringArray []string
|
||||
|
||||
func (p StringArray) Len() int { return len(p) }
|
||||
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
// Convenience wrappers for common cases
|
||||
func SortInts(a []int) { Sort(IntArray(a)) }
|
||||
func SortStrings(a []string) { Sort(StringArray(a)) }
|
||||
|
||||
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
|
||||
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
package sort
|
||||
|
||||
type Sorter interface {
|
||||
Len() int
|
||||
Less(i, j int) bool
|
||||
Swap(i, j int)
|
||||
}
|
||||
|
||||
/*
|
||||
func Sort(Sorter Interface) {
|
||||
for i := 1; i < data.Len(); i++ {
|
||||
for j := i; j > 0 && data.Less(j, j-1); j-- {
|
||||
data.Swap(j, j-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func Sort(data Sorter) {
|
||||
for pass := 1; pass < data.Len(); pass++ {
|
||||
for i := 0; i < data.Len()-pass; i++ {
|
||||
if data.Less(i+1, i) {
|
||||
data.Swap(i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsSorted(data Sorter) bool {
|
||||
n := data.Len()
|
||||
for i := n - 1; i > 0; i-- {
|
||||
if data.Less(i, i-1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Convenience types for common cases
|
||||
type IntArray []int
|
||||
|
||||
func (p IntArray) Len() int { return len(p) }
|
||||
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
type StringArray []string
|
||||
|
||||
func (p StringArray) Len() int { return len(p) }
|
||||
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
// Convenience wrappers for common cases
|
||||
func SortInts(a []int) { Sort(IntArray(a)) }
|
||||
func SortStrings(a []string) { Sort(StringArray(a)) }
|
||||
|
||||
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
|
||||
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
|
||||
|
@@ -1,36 +1,36 @@
|
||||
// sort_persons.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./sort"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
firstName string
|
||||
lastName string
|
||||
}
|
||||
|
||||
type Persons []Person
|
||||
|
||||
func (p Persons) Len() int { return len(p) }
|
||||
|
||||
func (p Persons) Less(i, j int) bool {
|
||||
in := p[i].lastName + " " + p[i].firstName
|
||||
jn := p[j].lastName + " " + p[j].firstName
|
||||
return in < jn
|
||||
}
|
||||
|
||||
func (p Persons) Swap(i, j int) {
|
||||
p[i], p[j] = p[j], p[i]
|
||||
}
|
||||
|
||||
func main() {
|
||||
p1 := Person{"Xavier","Papadopoulos"}
|
||||
p2 := Person{"Chris","Naegels"}
|
||||
p3 := Person{"John","Doe"}
|
||||
arrP := Persons{p1,p2,p3}
|
||||
fmt.Printf("Before sorting: %v\n", arrP)
|
||||
sort.Sort(arrP)
|
||||
fmt.Printf("After sorting: %v\n", arrP)
|
||||
}
|
||||
// sort_persons.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"./sort"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
firstName string
|
||||
lastName string
|
||||
}
|
||||
|
||||
type Persons []Person
|
||||
|
||||
func (p Persons) Len() int { return len(p) }
|
||||
|
||||
func (p Persons) Less(i, j int) bool {
|
||||
in := p[i].lastName + " " + p[i].firstName
|
||||
jn := p[j].lastName + " " + p[j].firstName
|
||||
return in < jn
|
||||
}
|
||||
|
||||
func (p Persons) Swap(i, j int) {
|
||||
p[i], p[j] = p[j], p[i]
|
||||
}
|
||||
|
||||
func main() {
|
||||
p1 := Person{"Xavier", "Papadopoulos"}
|
||||
p2 := Person{"Chris", "Naegels"}
|
||||
p3 := Person{"John", "Doe"}
|
||||
arrP := Persons{p1, p2, p3}
|
||||
fmt.Printf("Before sorting: %v\n", arrP)
|
||||
sort.Sort(arrP)
|
||||
fmt.Printf("After sorting: %v\n", arrP)
|
||||
}
|
||||
|
@@ -1,39 +1,39 @@
|
||||
// stack.go
|
||||
package stack
|
||||
|
||||
import "errors"
|
||||
|
||||
type Stack []interface{}
|
||||
|
||||
func (stack Stack) Len() int {
|
||||
return len(stack)
|
||||
}
|
||||
|
||||
func (stack Stack) Cap() int {
|
||||
return cap(stack)
|
||||
}
|
||||
|
||||
func (stack Stack) IsEmpty() bool {
|
||||
return len(stack) == 0
|
||||
}
|
||||
|
||||
func (stack *Stack) Push(e interface{}) {
|
||||
*stack = append(*stack, e)
|
||||
}
|
||||
|
||||
func (stack Stack) Top() (interface{}, error) {
|
||||
if len(stack) == 0 {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
return stack[len(stack)-1], nil
|
||||
}
|
||||
|
||||
func (stack *Stack) Pop() (interface{}, error) {
|
||||
stk := *stack // dereference to a local variable stk
|
||||
if len(stk) == 0 {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
top := stk[len(stk)-1]
|
||||
*stack = stk[:len(stk)-1] // shrink the stack
|
||||
return top, nil
|
||||
}
|
||||
// stack.go
|
||||
package stack
|
||||
|
||||
import "errors"
|
||||
|
||||
type Stack []interface{}
|
||||
|
||||
func (stack Stack) Len() int {
|
||||
return len(stack)
|
||||
}
|
||||
|
||||
func (stack Stack) Cap() int {
|
||||
return cap(stack)
|
||||
}
|
||||
|
||||
func (stack Stack) IsEmpty() bool {
|
||||
return len(stack) == 0
|
||||
}
|
||||
|
||||
func (stack *Stack) Push(e interface{}) {
|
||||
*stack = append(*stack, e)
|
||||
}
|
||||
|
||||
func (stack Stack) Top() (interface{}, error) {
|
||||
if len(stack) == 0 {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
return stack[len(stack)-1], nil
|
||||
}
|
||||
|
||||
func (stack *Stack) Pop() (interface{}, error) {
|
||||
stk := *stack // dereference to a local variable stk
|
||||
if len(stk) == 0 {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
top := stk[len(stk)-1]
|
||||
*stack = stk[:len(stk)-1] // shrink the stack
|
||||
return top, nil
|
||||
}
|
||||
|
@@ -1,28 +1,28 @@
|
||||
// stack_general_v2.go
|
||||
// Package collection implements a generic stack.
|
||||
package collection
|
||||
|
||||
// The zero value for Stack is an empty stack ready to use.
|
||||
type Stack struct {
|
||||
data []interface{}
|
||||
}
|
||||
|
||||
// Push adds x to the top of the stack.
|
||||
func (s *Stack) Push(x interface{}) {
|
||||
s.data = append(s.data, x)
|
||||
}
|
||||
|
||||
// Pop removes and returns the top element of the stack.
|
||||
// It's a run-time error to call Pop on an empty stack.
|
||||
func (s *Stack) Pop() interface{} {
|
||||
i := len(s.data) - 1
|
||||
res := s.data[i]
|
||||
s.data[i] = nil // to avoid memory leak
|
||||
s.data = s.data[:i]
|
||||
return res
|
||||
}
|
||||
|
||||
// Size returns the number of elements in the stack.
|
||||
func (s *Stack) Size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
// stack_general_v2.go
|
||||
// Package collection implements a generic stack.
|
||||
package collection
|
||||
|
||||
// The zero value for Stack is an empty stack ready to use.
|
||||
type Stack struct {
|
||||
data []interface{}
|
||||
}
|
||||
|
||||
// Push adds x to the top of the stack.
|
||||
func (s *Stack) Push(x interface{}) {
|
||||
s.data = append(s.data, x)
|
||||
}
|
||||
|
||||
// Pop removes and returns the top element of the stack.
|
||||
// It's a run-time error to call Pop on an empty stack.
|
||||
func (s *Stack) Pop() interface{} {
|
||||
i := len(s.data) - 1
|
||||
res := s.data[i]
|
||||
s.data[i] = nil // to avoid memory leak
|
||||
s.data = s.data[:i]
|
||||
return res
|
||||
}
|
||||
|
||||
// Size returns the number of elements in the stack.
|
||||
func (s *Stack) Size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user