go知识重新整理

This commit is contained in:
Estom
2021-09-03 05:34:34 +08:00
parent 62309f856a
commit 1bad082e49
291 changed files with 29345 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
/**
* @Author:zhoutao
* @Date:2020/12/11 下午2:03
* @Desc:
*/
package proxy
//代理模式:用于延迟处理操作或者进行实际操作前后进行其他处理
//代理模式的常见用法虚代理、COW代理、远程代理、保护代理、Cache代理、防火墙代理、同步代理、智能指引等
type Subject interface {
Do() string
}
type RealSubject struct {
}
func (RealSubject) Do() string {
return "going to do something"
}
type Proxy struct {
real RealSubject
}
func (p Proxy) Do() string {
var res string
//在调用想要做的工作之前,做一些事情,如检查缓存、判断权限、实例化真实对象等
res += "pre to do :"
//实际要做的事情
res += p.real.Do()
//调用之后执行的操作,如缓存结果,对结果进行处理等
res += ": after to do"
return res
}

View File

@@ -0,0 +1,20 @@
/**
* @Author:zhoutao
* @Date:2020/12/11 下午2:13
* @Desc:
*/
package proxy
import "testing"
var expect = "pre to do :going to do something: after to do"
func TestProxy(t *testing.T) {
var sub Subject
sub = &Proxy{}
res := sub.Do()
if res != expect {
t.Fatalf("expect %s ,return %s", expect, res)
}
}