Sync
This commit is contained in:
parent
cfd7ab8a0b
commit
cd080ddda2
12
tour_of_go/l5/3
Normal file
12
tour_of_go/l5/3
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i interface{} = "hello"
|
||||||
|
|
||||||
|
s := i.(string)
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
fmt.Printf("%v- %T\n", s, s)
|
||||||
|
}
|
18
tour_of_go/l5/empty-interface.go
Normal file
18
tour_of_go/l5/empty-interface.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func describe(i interface{}) {
|
||||||
|
fmt.Printf("(%v, %T)\n", i, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i interface{}
|
||||||
|
describe(i)
|
||||||
|
|
||||||
|
i = 42
|
||||||
|
describe(i)
|
||||||
|
|
||||||
|
i = "hello"
|
||||||
|
describe(i)
|
||||||
|
}
|
28
tour_of_go/l5/error.go
Normal file
28
tour_of_go/l5/error.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyError struct {
|
||||||
|
When time.Time
|
||||||
|
What string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *MyError) Error() string {
|
||||||
|
return fmt.Sprintf("at %v, %s", e.When, e.What)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() error {
|
||||||
|
return &MyError {
|
||||||
|
time.Now(),
|
||||||
|
"it didn't work",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := run(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
16
tour_of_go/l5/ex-reader/exercise-reader.go
Normal file
16
tour_of_go/l5/ex-reader/exercise-reader.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "golang.org/x/tour/reader"
|
||||||
|
|
||||||
|
type MyReader struct{}
|
||||||
|
|
||||||
|
func (r MyReader) Read(b []byte) (int, error) {
|
||||||
|
for i := range(b) {
|
||||||
|
b[i] = 'A'
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
reader.Validate(MyReader{})
|
||||||
|
}
|
30
tour_of_go/l5/ex-reader/exercise-rot-reader.go
Normal file
30
tour_of_go/l5/ex-reader/exercise-rot-reader.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type rot13Reader struct {
|
||||||
|
r io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r rot13Reader) Read(b []byte) (int, error) {
|
||||||
|
n, err := r.r.Read(b)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
switch {
|
||||||
|
case 'A' <= b[i] && 'Z' >= b[i]:
|
||||||
|
b[i] = 'A' + (b[i]-'A'+13)%26
|
||||||
|
case 'a' <= b[i] && 'z' >= b[i]:
|
||||||
|
b[i] = 'a' + (b[i]-'a'+13)%26
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := strings.NewReader("Lbh penpxrq gur pbqr!")
|
||||||
|
r := rot13Reader{s}
|
||||||
|
io.Copy(os.Stdout, &r)
|
||||||
|
}
|
5
tour_of_go/l5/ex-reader/go.mod
Normal file
5
tour_of_go/l5/ex-reader/go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module exercies-reader.go
|
||||||
|
|
||||||
|
go 1.24.5
|
||||||
|
|
||||||
|
require golang.org/x/tour v0.1.0
|
2
tour_of_go/l5/ex-reader/go.sum
Normal file
2
tour_of_go/l5/ex-reader/go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE=
|
||||||
|
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc=
|
41
tour_of_go/l5/exercise-errors.go
Normal file
41
tour_of_go/l5/exercise-errors.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ErrNegativeSqrt float64
|
||||||
|
|
||||||
|
func (e ErrNegativeSqrt) Error() string {
|
||||||
|
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sqrt(x float64) (float64, error) {
|
||||||
|
if x < 0 {
|
||||||
|
return 0, ErrNegativeSqrt(x)
|
||||||
|
}
|
||||||
|
precision := 0.00001
|
||||||
|
old_z := 0.0
|
||||||
|
z := x
|
||||||
|
iter := 0
|
||||||
|
for (z - old_z) > precision {
|
||||||
|
iter += 1
|
||||||
|
old_z := z
|
||||||
|
z -= (z*z - x) / (2*z)
|
||||||
|
if old_z - z < 0 {
|
||||||
|
if ((old_z - z) * -1) < precision {
|
||||||
|
fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
|
||||||
|
return z, nil
|
||||||
|
}
|
||||||
|
} else if (old_z - z) < precision {
|
||||||
|
fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
|
||||||
|
return z, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return z, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(Sqrt(2))
|
||||||
|
fmt.Println(Sqrt(-2))
|
||||||
|
}
|
19
tour_of_go/l5/exercise-stringer.go
Normal file
19
tour_of_go/l5/exercise-stringer.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type IPAddr [4]byte
|
||||||
|
|
||||||
|
func (i IPAddr) String() string {
|
||||||
|
return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3])
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hosts := map[string]IPAddr{
|
||||||
|
"loopback": {127, 0, 0, 1},
|
||||||
|
"googleDNS": {8, 8, 8, 8},
|
||||||
|
}
|
||||||
|
for name, ip := range hosts {
|
||||||
|
fmt.Printf("%v: %v\n", name, ip)
|
||||||
|
}
|
||||||
|
}
|
30
tour_of_go/l5/indirection.go
Normal file
30
tour_of_go/l5/indirection.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Vertex) Scale(f float64) {
|
||||||
|
v.X = v.X * f
|
||||||
|
v.Y = v.Y * f
|
||||||
|
}
|
||||||
|
|
||||||
|
func ScaleFunc(v *Vertex, f float64) {
|
||||||
|
v.X = v.X * f
|
||||||
|
v.Y = v.Y * f
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := Vertex{3, 4}
|
||||||
|
v.Scale(2)
|
||||||
|
ScaleFunc(&v, 10)
|
||||||
|
|
||||||
|
p := &Vertex{4, 3}
|
||||||
|
p.Scale(3)
|
||||||
|
ScaleFunc(p, 8)
|
||||||
|
|
||||||
|
fmt.Println(v, p)
|
||||||
|
}
|
||||||
|
|
51
tour_of_go/l5/interfaces-continued.go
Normal file
51
tour_of_go/l5/interfaces-continued.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type I interface {
|
||||||
|
M()
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
S string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) M() {
|
||||||
|
if t == nil {
|
||||||
|
fmt.Println("<nil>")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(t.S)
|
||||||
|
}
|
||||||
|
|
||||||
|
type F float64
|
||||||
|
|
||||||
|
func (f F) M() {
|
||||||
|
fmt.Println(f)
|
||||||
|
}
|
||||||
|
func describe(i I) {
|
||||||
|
fmt.Printf("(%v, %T)\n", i, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i I
|
||||||
|
// These lines will cause a run-time error due to nil pointer dereferencing
|
||||||
|
// describe(i)
|
||||||
|
// i.M()
|
||||||
|
|
||||||
|
var t *T
|
||||||
|
i = t
|
||||||
|
describe(i)
|
||||||
|
i.M()
|
||||||
|
|
||||||
|
i = &T{"hello"}
|
||||||
|
describe(i)
|
||||||
|
i.M()
|
||||||
|
|
||||||
|
i = F(math.Pi)
|
||||||
|
describe(i)
|
||||||
|
i.M()
|
||||||
|
}
|
40
tour_of_go/l5/interfaces.go
Normal file
40
tour_of_go/l5/interfaces.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyFloat float64
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Abser interface {
|
||||||
|
Abs() float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f MyFloat) Abs() float64 {
|
||||||
|
if f < 0 {
|
||||||
|
return float64(-f)
|
||||||
|
}
|
||||||
|
return float64(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Vertex) Abs() float64 {
|
||||||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var a Abser
|
||||||
|
f := MyFloat(-math.Sqrt2)
|
||||||
|
v := Vertex{3, 4}
|
||||||
|
|
||||||
|
a = f
|
||||||
|
a = &v
|
||||||
|
|
||||||
|
// a = v
|
||||||
|
|
||||||
|
fmt.Println(a.Abs())
|
||||||
|
}
|
20
tour_of_go/l5/methods-continued.go
Normal file
20
tour_of_go/l5/methods-continued.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyFloat float64
|
||||||
|
|
||||||
|
func (f MyFloat) Abs() float64 {
|
||||||
|
if f < 0 {
|
||||||
|
return float64(-f)
|
||||||
|
}
|
||||||
|
return float64(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
f := MyFloat(-math.Sqrt2)
|
||||||
|
fmt.Println(f.Abs())
|
||||||
|
}
|
25
tour_of_go/l5/methods-pointers.go
Normal file
25
tour_of_go/l5/methods-pointers.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v Vertex) Abs() float64 {
|
||||||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Vertex) Scale(f float64) {
|
||||||
|
v.X = v.X * f
|
||||||
|
v.Y = v.Y * f
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := Vertex{3, 4}
|
||||||
|
v.Scale(10)
|
||||||
|
fmt.Println(v.Abs())
|
||||||
|
}
|
29
tour_of_go/l5/methods.go
Normal file
29
tour_of_go/l5/methods.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a method - a function that implements a receiver (the part between "func" and "Abs()")
|
||||||
|
|
||||||
|
func (v Vertex) Abs() float64 {
|
||||||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The method above has the same functionality as the function below
|
||||||
|
|
||||||
|
func Abs(v Vertex) float64 {
|
||||||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := Vertex{3,4}
|
||||||
|
fmt.Println(v.Abs())
|
||||||
|
w := Vertex{3,4}
|
||||||
|
fmt.Println(Abs(w))
|
||||||
|
}
|
25
tour_of_go/l5/pointers-functions.go
Normal file
25
tour_of_go/l5/pointers-functions.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func Abs(v Vertex) float64 {
|
||||||
|
return math.Sqrt(v.X*v.X + v.Y*v.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Scale(v Vertex, f float64) {
|
||||||
|
v.X = v.X * f
|
||||||
|
v.Y = v.Y * f
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := Vertex{3, 4}
|
||||||
|
Scale(v, 10)
|
||||||
|
fmt.Println(Abs(v))
|
||||||
|
}
|
21
tour_of_go/l5/reader.go
Normal file
21
tour_of_go/l5/reader.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := strings.NewReader("Hello, Reader!")
|
||||||
|
|
||||||
|
b := make([]byte, 8)
|
||||||
|
for {
|
||||||
|
n, err := r.Read(b)
|
||||||
|
fmt.Printf("n = %v err = %v b = %v\n", n, err, b)
|
||||||
|
fmt.Printf("b[:n] = %q\n", b[:n])
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
tour_of_go/l5/stringer.go
Normal file
18
tour_of_go/l5/stringer.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Person) String() string {
|
||||||
|
return fmt.Sprintf("%v (%v years)\n", p.Name, p.Age)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := Person{"Arthur The King", 51}
|
||||||
|
b := Person{"Darth Vader", 49}
|
||||||
|
fmt.Println(a, b)
|
||||||
|
}
|
22
tour_of_go/l5/type-assertions.go
Normal file
22
tour_of_go/l5/type-assertions.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i interface{} = "hello"
|
||||||
|
|
||||||
|
s := i.(string)
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
s, ok := i.(string)
|
||||||
|
fmt.Println(s, ok)
|
||||||
|
|
||||||
|
f, ok := i.(float64)
|
||||||
|
fmt.Println(f, ok)
|
||||||
|
|
||||||
|
// This will cause a panic
|
||||||
|
// f = i.(float64)
|
||||||
|
// fmt.Println(f)
|
||||||
|
|
||||||
|
fmt.Printf("Type info: %v- %T\n", s, s)
|
||||||
|
}
|
20
tour_of_go/l5/type-switches.go
Normal file
20
tour_of_go/l5/type-switches.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func do(i interface{}) {
|
||||||
|
switch v := i.(type) {
|
||||||
|
case int:
|
||||||
|
fmt.Printf("Twice %v is %v\n", v, v*2)
|
||||||
|
case string:
|
||||||
|
fmt.Printf("%q is %v bytes long\n", v, len(v))
|
||||||
|
default:
|
||||||
|
fmt.Printf("I don't know about this type %T!\n", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
do(21)
|
||||||
|
do("Golang")
|
||||||
|
do(true)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user