day8方法 剩余全部内容
parent
bb4af455d5
commit
6dc5617136
|
@ -17,9 +17,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := r.URL.Query()
|
//vars := r.URL.Query()
|
||||||
a := vars["a"][0]
|
//a := vars["a"][0]
|
||||||
fmt.Fprintf(w, a)
|
//fmt.Fprintf(w, a)
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
count++
|
count++
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Study/src/study/day8Method/common"
|
||||||
|
"fmt"
|
||||||
|
"image/color"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var red = color.RGBA{
|
||||||
|
R: 255,
|
||||||
|
G: 0,
|
||||||
|
B: 0,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
|
var green = color.RGBA{
|
||||||
|
R: 0,
|
||||||
|
G: 255,
|
||||||
|
B: 0,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
|
var blue = color.RGBA{
|
||||||
|
R: 0,
|
||||||
|
G: 0,
|
||||||
|
B: 255,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
|
var cp common.ColoredPoint = common.ColoredPoint{
|
||||||
|
common.Point{X: 0, Y: 0},
|
||||||
|
red,
|
||||||
|
}
|
||||||
|
cp.X = 255
|
||||||
|
cp.Y = 233
|
||||||
|
|
||||||
|
var greenCP common.ColoredPoint
|
||||||
|
greenCP.X = 0
|
||||||
|
greenCP.Y = 0
|
||||||
|
greenCP.Color = green
|
||||||
|
|
||||||
|
// 由于ColoredPoint包含了Point,ColoredPoint也包含了Point的方法
|
||||||
|
fmt.Println(cp.Distance(greenCP.Point))
|
||||||
|
|
||||||
|
// 如果新的结构体包含的是一个另一个结构体指针类型
|
||||||
|
|
||||||
|
var blueCP = common.ColoredPointPoint{
|
||||||
|
P: &common.Point{X: 233, Y: 123},
|
||||||
|
RGBA: blue,
|
||||||
|
}
|
||||||
|
var blackCP = common.ColoredPointPoint{
|
||||||
|
P: &common.Point{X: 0, Y: 0},
|
||||||
|
RGBA: color.RGBA{
|
||||||
|
R: 0,
|
||||||
|
G: 0,
|
||||||
|
B: 0,
|
||||||
|
A: 255,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// 这种情况下字段和方法会被间接地引入到当前的类型中
|
||||||
|
// (译注:访问需要通过该指针指向的对象去取)。
|
||||||
|
// 添加这一层间接关系让我们可以共享通用的结构并动态地改变对象之间的关系。
|
||||||
|
fmt.Println(blueCP.P.Distance(*blackCP.P))
|
||||||
|
// 注意 如果是显式的定义了变量名,
|
||||||
|
// 你访问包含的其他结构体的方法也要带上变量名
|
||||||
|
|
||||||
|
var (
|
||||||
|
i int = 233
|
||||||
|
www string = "123233"
|
||||||
|
)
|
||||||
|
fmt.Println(i, www)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var cache = struct {
|
||||||
|
sync.Mutex
|
||||||
|
mapping map[string]string
|
||||||
|
}{
|
||||||
|
mapping: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
func Lookup(key string) string {
|
||||||
|
cache.Lock()
|
||||||
|
v := cache.mapping[key]
|
||||||
|
cache.Unlock()
|
||||||
|
return v
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Study/src/study/day8Method/common"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
// nil也是一个合法的接受者
|
||||||
|
// 就像一些函数允许nil指针作为参数一样,
|
||||||
|
// 方法理论上也可以用nil指针作为其接收器,
|
||||||
|
// 尤其当nil对于对象来说是合法的零值时,比如map或者slice。
|
||||||
|
// 在下面的简单int链表的例子里,nil代表的是空链表:
|
||||||
|
|
||||||
|
// An IntList is a linked list of integers.
|
||||||
|
// A nil *IntList represents the empty list.
|
||||||
|
type IntList struct {
|
||||||
|
Value int
|
||||||
|
Tail *IntList
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum returns the sum of the list elements.
|
||||||
|
func (list *IntList) Sum() int {
|
||||||
|
if list == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return list.Value + list.Tail.Sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := common.Point{X: 1, Y: 1}
|
||||||
|
// 指针对象的方法
|
||||||
|
Ppoint := &a
|
||||||
|
Ppoint.ScaleBy(4)
|
||||||
|
fmt.Println(Ppoint)
|
||||||
|
fmt.Println(a)
|
||||||
|
// 我们也可以直接使用非指针变量调用
|
||||||
|
// go内部处理时候会自动取地址
|
||||||
|
a.ScaleBy(-1)
|
||||||
|
fmt.Println(a)
|
||||||
|
// 临时变量是不可以得到地址的
|
||||||
|
//Point{X: 1, Y: 1}.ScaleBy(1) // 报错
|
||||||
|
|
||||||
|
// nil也可以是接受者
|
||||||
|
m := url.Values{"lang": {"en"}} // direct construction
|
||||||
|
m.Add("item", "1")
|
||||||
|
m.Add("item", "2")
|
||||||
|
|
||||||
|
fmt.Println(m.Get("lang")) // "en"
|
||||||
|
fmt.Println(m.Get("q")) // ""
|
||||||
|
fmt.Println(m.Get("item")) // "1" (first value)
|
||||||
|
fmt.Println(m["item"]) // "[1 2]" (direct map access)
|
||||||
|
|
||||||
|
m = nil
|
||||||
|
fmt.Println(m.Get("item")) // ""
|
||||||
|
m.Add("item", "3") // panic: assignment to entry in nil map
|
||||||
|
// 相当于
|
||||||
|
url.Values(nil).Get("item")
|
||||||
|
// 但是如果你直接写nil.Get("item")的话是无法通过编译的,
|
||||||
|
// 因为nil的字面量编译器无法判断其准确类型。
|
||||||
|
}
|
|
@ -0,0 +1,200 @@
|
||||||
|
package bitint
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IntSet struct {
|
||||||
|
// 将words从uint64切片改为uint切片,以适应32位和64位平台
|
||||||
|
words []uint
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PlatformCompatible 用于适应32位和64位平台
|
||||||
|
PlatformCompatible = 32 << (^uint(0) >> 63)
|
||||||
|
|
||||||
|
// 将代码中所有的64改为PlatformCompatible, 就完成了适配
|
||||||
|
)
|
||||||
|
|
||||||
|
// 展示一下效果
|
||||||
|
|
||||||
|
func (s *IntSet) Has(x int) bool {
|
||||||
|
word, bit := x/PlatformCompatible, uint(x%PlatformCompatible)
|
||||||
|
fmt.Println("word, bit: ", word, bit)
|
||||||
|
return word < len(s.words) && s.words[word]&(1<<bit) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntSet) Add(x int) {
|
||||||
|
word, bit := x/PlatformCompatible, uint(x%PlatformCompatible)
|
||||||
|
for word >= len(s.words) {
|
||||||
|
s.words = append(s.words, 0)
|
||||||
|
}
|
||||||
|
s.words[word] |= 1 << bit
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntSet) AddAll(nums ...int) {
|
||||||
|
for _, num := range nums {
|
||||||
|
s.Add(num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Elems returns a slice containing the elements of the set.
|
||||||
|
// Elems 返回一个包含集合中所有元素的切片。
|
||||||
|
func (s *IntSet) Elems() (elems []int) {
|
||||||
|
// 创建一个空的切片
|
||||||
|
//var elems []int
|
||||||
|
// 遍历s.words
|
||||||
|
for i, word := range s.words {
|
||||||
|
// 判断word是否为空
|
||||||
|
if word == 0 {
|
||||||
|
// 如果为空, 则跳过
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 遍历word
|
||||||
|
for j := 0; j < PlatformCompatible; j++ {
|
||||||
|
// 判断word的第j位是否为1
|
||||||
|
if word&(1<<uint(j)) != 0 {
|
||||||
|
// 如果为1, 则将PlatformCompatible*i+j添加到elems中
|
||||||
|
elems = append(elems, PlatformCompatible*i+j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 返回elems
|
||||||
|
return elems
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntSet) UnionWith(t *IntSet) {
|
||||||
|
for i, tword := range t.words {
|
||||||
|
if i < len(s.words) {
|
||||||
|
s.words[i] |= tword
|
||||||
|
} else {
|
||||||
|
s.words = append(s.words, tword)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s IntSet) Show() {
|
||||||
|
fmt.Printf("%v\n", s.words)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntersectWith computes the intersection of s and t.
|
||||||
|
// 这个不解释了=_=|
|
||||||
|
func (s *IntSet) IntersectWith(t *IntSet) {
|
||||||
|
// 遍历s.words
|
||||||
|
for i, word := range s.words {
|
||||||
|
// 判断i是否越界
|
||||||
|
if i >= len(t.words) {
|
||||||
|
// 如果越界, 则将word置为0
|
||||||
|
// 越界代表t中没有这个word, 与任何数都是0, 所以将s中的word置为0
|
||||||
|
s.words[i] = 0
|
||||||
|
} else {
|
||||||
|
// 如果没有越界, 则将s.words[i]和t.words[i]进行与运算
|
||||||
|
// 与运算的结果就是s和t的交集
|
||||||
|
s.words[i] = word & t.words[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DifferenceWith computes the difference of s and t.
|
||||||
|
// s集扣掉s与t的交集就是s与t的差集
|
||||||
|
func (s *IntSet) DifferenceWith(t *IntSet) {
|
||||||
|
// 遍历s.words
|
||||||
|
for i, word := range s.words {
|
||||||
|
// 判断i是否越界
|
||||||
|
if i >= len(t.words) {
|
||||||
|
// 如果越界, 则跳过
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 如果没有越界, 则将s.words[i]和t.words[i]进行异或运算
|
||||||
|
// 异或运算的结果就是s和t的差集
|
||||||
|
s.words[i] = word ^ t.words[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SymmetricDifference computes the symmetric difference of s and t.
|
||||||
|
// s和t的并集扣掉他们的交集就是差并集(对称差集)
|
||||||
|
func (s *IntSet) SymmetricDifference(t *IntSet) {
|
||||||
|
// 创建一个空的IntSet
|
||||||
|
var u IntSet
|
||||||
|
// 将s和t的并集赋值给u, 相当于u里面有s和t的所有元素
|
||||||
|
u.UnionWith(s)
|
||||||
|
u.UnionWith(t)
|
||||||
|
// 将s和t的交集赋值给s, 相当于s中只有s和t中共同都有的元素了, 这也就是交集
|
||||||
|
s.IntersectWith(t)
|
||||||
|
// 将u和s的差集赋值给u, 相当于u扣掉了s和t的交集
|
||||||
|
u.DifferenceWith(s)
|
||||||
|
// 将u赋值给s
|
||||||
|
*s = u
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the set as a string of the form "{1 2 3}".
|
||||||
|
func (s *IntSet) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.WriteByte('{')
|
||||||
|
for i, word := range s.words {
|
||||||
|
if word == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for j := 0; j < PlatformCompatible; j++ {
|
||||||
|
if word&(1<<uint(j)) != 0 {
|
||||||
|
if buf.Len() > len("{") {
|
||||||
|
buf.WriteByte(' ')
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "%d", PlatformCompatible*i+j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf.WriteByte('}')
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the number of elements.
|
||||||
|
func (s *IntSet) Len() (l int) {
|
||||||
|
//遍历s.words
|
||||||
|
for _, word := range s.words {
|
||||||
|
// 判断是否为空切片
|
||||||
|
if word == 0 {
|
||||||
|
// 为空则跳过
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 不为空则遍历word
|
||||||
|
for j := 0; j < PlatformCompatible; j++ {
|
||||||
|
// 判断word的第j位是否为1
|
||||||
|
// 如果为1, 则代表这个位置存在数字
|
||||||
|
if word&(1<<uint(j)) != 0 {
|
||||||
|
// 为1则l++
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 返回l
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove removes x from the set.
|
||||||
|
func (s *IntSet) Remove(x int) {
|
||||||
|
// 取得x的word和bit
|
||||||
|
word, bit := x/PlatformCompatible, uint(x%PlatformCompatible)
|
||||||
|
// 判断word是否越界
|
||||||
|
if word < len(s.words) {
|
||||||
|
// 如果没有越界, 则将word的第bit位置为0
|
||||||
|
s.words[word] &= ^(1 << bit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear removes all elements from the set.
|
||||||
|
func (s *IntSet) Clear() {
|
||||||
|
// 将s.words置为nil
|
||||||
|
s.words = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy returns a copy of the set.
|
||||||
|
func (s *IntSet) Copy() *IntSet {
|
||||||
|
// 创建一个新的IntSet
|
||||||
|
var newIntSet IntSet
|
||||||
|
// 将s.words赋值给new.words
|
||||||
|
newIntSet.words = append(newIntSet.words, s.words...)
|
||||||
|
// 返回newIntSet
|
||||||
|
return &newIntSet
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import "image/color"
|
||||||
|
|
||||||
|
// 注意 如果是显式的定义了变量名,
|
||||||
|
// 你访问包含的其他结构体的方法也要带上变量名
|
||||||
|
type ColoredPointPoint struct {
|
||||||
|
P *Point
|
||||||
|
color.RGBA
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Point) Add(q Point) Point { return Point{p.X + q.X, p.Y + q.Y} }
|
||||||
|
func (p Point) Sub(q Point) Point { return Point{p.X - q.X, p.Y - q.Y} }
|
||||||
|
|
||||||
|
func (path Path) TranslateBy(offset Point, add bool) {
|
||||||
|
var op func(p, q Point) Point
|
||||||
|
if add {
|
||||||
|
op = Point.Add
|
||||||
|
} else {
|
||||||
|
op = Point.Sub
|
||||||
|
}
|
||||||
|
for i := range path {
|
||||||
|
// Call either path[i].Add(offset) or path[i].Sub(offset).
|
||||||
|
path[i] = op(path[i], offset)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 (p *Point) ScaleBy(factor float64) {
|
||||||
|
p.X *= factor
|
||||||
|
p.Y *= factor
|
||||||
|
}
|
||||||
|
|
||||||
|
// ColoredPoint 这个结构体包含了Point这个结构体
|
||||||
|
// 同时也获得了Point的方法
|
||||||
|
type ColoredPoint struct {
|
||||||
|
Point
|
||||||
|
Color color.RGBA
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Study/src/study/day8Method/common"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i myInt = 1
|
||||||
|
i.MethodStart()
|
||||||
|
a := common.Point{X: 233, Y: 233}
|
||||||
|
b := common.Point{X: 3, Y: 3}
|
||||||
|
|
||||||
|
// 传统函数也能够轻松实现此功能
|
||||||
|
fmt.Println(common.Distance(a, b))
|
||||||
|
// 但是使用 方法就无疑轻松很多了
|
||||||
|
fmt.Println(a.Distance(b))
|
||||||
|
|
||||||
|
perim := common.Path{
|
||||||
|
{1, 1},
|
||||||
|
{5, 1},
|
||||||
|
{5, 4},
|
||||||
|
{1, 1},
|
||||||
|
}
|
||||||
|
fmt.Println(perim.Distance()) // "12"
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue