day8方法 剩余全部内容

master
独孤伶俜 2022-12-13 23:03:56 +08:00
parent 6dc5617136
commit 2e765820cd
4 changed files with 29 additions and 48 deletions

6
go.mod
View File

@ -2,4 +2,8 @@ module Study
go 1.19
require golang.org/x/net v0.2.0 // indirect
require (
github.com/arl/statsviz v0.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/net v0.2.0 // indirect
)

4
go.sum
View File

@ -1,2 +1,6 @@
github.com/arl/statsviz v0.5.1 h1:3HY0ZEB738JtguWsD1Tf1pFJZiCcWUmYRq/3OTYKaSI=
github.com/arl/statsviz v0.5.1/go.mod h1:zDnjgRblGm1Dyd7J5YlbH7gM1/+HRC+SfkhZhQb5AnM=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"math"
)
// 在函数声明时, 在其函数名之前放上一个变量, 即是一个方法
@ -24,49 +23,4 @@ func (i myInt) MethodStart() {
}
// Point 下面是个例子
type Point struct{ X, Y float64 }
// Distance traditional function
func Distance(p, q Point) float64 {
return math.Hypot(q.X-p.X, q.Y-p.Y)
}
// Path 不同的类型可以有同名的方法, 不同类型的调用,将会指向不同的方法, 实现不同的功能
// A Path is a journey connecting the points with straight lines.
type Path []Point
// Distance returns the distance traveled along the path.
func (path Path) Distance() float64 {
sum := 0.0
for i := range path {
if i > 0 {
sum += path[i-1].Distance(path[i])
}
}
return sum
}
// Distance same thing, but as a method of the Point type
func (p Point) Distance(q Point) float64 {
return math.Hypot(q.X-p.X, q.Y-p.Y)
}
func main() {
var i myInt = 1
i.MethodStart()
a := Point{X: 233, Y: 233}
b := Point{X: 3, Y: 3}
// 传统函数也能够轻松实现此功能
fmt.Println(Distance(a, b))
// 但是使用 方法就无疑轻松很多了
fmt.Println(a.Distance(b))
perim := Path{
{1, 1},
{5, 1},
{5, 4},
{1, 1},
}
fmt.Println(perim.Distance()) // "12"
}
type Point1 struct{ X, Y float64 }

View File

@ -0,0 +1,19 @@
package main
import (
"Study/src/study/day8Method/bitint"
"fmt"
)
func main() {
var bit bitint.IntSet
bit.Show()
bit.Add(1)
bit.Add(2)
bit.Add(3)
bit.Show()
bit.Remove(2)
bit.Show()
fmt.Println(32 << (^uint(0) >> 63))
}