typetimeoutinterface{Timeout()bool}typetimeErrorstruct{}func(t *timeError)Error()string{return"the err is timeout"}func(t *timeError)Timeout()bool{returntrue}funcPublicFunc()error{return&timeError{}}functestError(errerror){// fmt.Println("error type:", reflect.TypeOf(err))e, ok := er...
type Upgrader struct{HandshakeTimeout time.Duration ReadBufferSize,WriteBufferSize int WriteBufferPool BufferPool Subprotocols[]string Errorfunc(w http.ResponseWriter,r*http.Request,status int,reason error)CheckOriginfunc(r*http.Request)bool EnableCompression bool} HandshakeTimeout: 握手完成的持续时间 Rea...
1// 请求失败造成 panic2funcmain(){3resp,err:=http.Get("https://api.ipify.org?format=json")4defer resp.Body.Close()// resp 可能为 nil,不能读取 Body5iferr!=nil{6fmt.Println(err)7return8}910body,err:=ioutil.ReadAll(resp.Body)11checkError(err)1213fmt.Println(string(body))14}1516fu...
// 即使写入超时,返回值n也可能>0,说明成功写入了部分数据SetWriteDeadline(ttime.Time)error} 然后每种类型都是对应的结构体实现这些接口。 还有一个常用的接口定义PacketConn typePacketConninterface { //ReadFrom方法从连接读取一个数据包,并将有效信息写入b//ReadFrom方法可能会在超过某个固定时间限制后超时返...
近期对nmap的操作系统识别功能造了个轮子,用golang实现了一遍,想未来能用于扫描器,资产发现/管理系统,网络空间引擎中。 造轮子也是一次深入理解它原理的过程,造完轮子后感觉到所有代码尽在我掌握之中,之后大规模扫描测试就可以 以最有效率,发最小包,绕过防火墙的方式进行集成,也能轻易的进行扩展。
type ServiceImplIOCInterface interface { GetHelloString(name string) string} 专属接口的命名为 $(结构名)IOCInterface,专属接口包含了结构的全部方法。专属接口的作用有二:1、减轻开发者工作量,方便直接通过 API 的方式 Get 到代理结构,方便直接作为字段注入。2、结构专属接口可以直接定位结构 ID,因此在注...
$ go tool pprofCPU.outFile:bench.testType:CPUTime:Dec24,2023at10:43am(CST)Duration:1.96s,Total samples=1.83s(93.33%)Entering interactivemode(type"help"forcommands,"o"foroptions)(pprof) 可视化界面分析: 使用go tool pprof -http=ip:port 启动服务。
checkErr(err) func checkErr(err error) { if err != nil { panic(err) } } 当然, 有人反对 checkErr 的写法: Why You Should Not Use checkErr, 这里不讨论偏好, 关注的是: 这样写严谨吗? 此nil 非彼 nil 这里我们需要一个自定义的错误(实现了error interface): type CustomError struct {} fun...
type MyError struct { msg string } func (e *MyError) Error() string { return e.msg } func checkError(e error) { if e != nil { println("false") } else { println("true") } } func main() { var err *MyError = nil checkError(err) // e == nil ? } 在main 函数中,我们...
对于go1.6以上版本,如果出现【并发map读写】程序会直接以fatal error崩溃,即使同routine内有recover()也不能恢复。如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误多个协程同时写会出现fatal error: concurrent map writes的错误 ...