使用了beego框架用toolbox.NewTask定时去执行一个func.
func代码如下
func Bocais() error {
//fmt.Println(time.Now().Format("2006/1/2 15:04:05"))
url := "https://www.7454cc.com/Lottery/Autocqssc"
payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"__RequestVerificationToken\"\r\n\r\ns3G01KZ8H9YSlU_HwS-5AUVZHMotO8Hta0SzQ12BJIHl7GspdCu8L4CoCjvDk2wt_tgvEt7xGIbBtMjy0xJTomJ0hoe9JxHIbLSIavfMDn-WmxE_GCY_rtZoBNfcZiQ7CU9agpxbxdg0zyBwTmM5xkK-1bvNgIibpslBBSL6FE41\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"class1\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
req.Header.Add("Accept", "application/json")
req.Header.Add("Cookie", "ASP.NET_SessionId=onab1ylbwmez1ld5j420r0n5; __RequestVerificationToken=mKcx8gEZ7M66CZa6Uqm9VhQfuc79iQqqaZZ1FFIZwYYlFuLyvEqeA_a3Z145PxiCSIf73QOk-jsOtUir1gHnNjdTFAR-kxLZXWTy2MAJGhRYTfPUFuPYvgo0KRw61DjHZAMAqEvURVlXYxXgh4OJOg2")
req.Header.Add("cache-control", "no-cache")
res, err := http.DefaultClient.Do(req)
defer res.Body.Close()
if err != nil {
fmt.Println("http请求失败")
return err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("body解析失败:" + string(body[:]))
return err
}
e := controllers.BocaiCache(body)
if e != nil {
fmt.Println(e)
}
return nil
}
BocaiCache方法如下
var i = 0
var number = ""
func BocaiCache(body []byte) error {
i++
fmt.Println(i)
exp := regexp.MustCompile(`"hm":\[(.+)\],`)
result1 := exp.FindAllSubmatch(body, -1)
str := string(result1[0][1])
if number != str {
fmt.Print("oldNumber:"+number+"->newNumber:"+str)
number = str
}
return nil
}
运行了大概一个半小时报错
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x827177]
goroutine 3869 [running]:
main.Bocais(0x0, 0x0)
C:/Users/Administrator/Desktop/go/src/bocai/main.go:37 +0x1d7
github.com/astaxie/beego/toolbox.(*Task).Run(0xc42011c180, 0xbf2580, 0x6e96bccddb4)
C:/Users/Administrator/Desktop/go/src/github.com/astaxie/beego/toolbox/task.go:143 +0x32
created by github.com/astaxie/beego/toolbox.run
C:/Users/Administrator/Desktop/go/src/github.com/astaxie/beego/toolbox/task.go:424 +0x1fd
截图如下
![微信截图_20181202200847.png](https://static.studygolang.com/181202/b01fd88e596126687f1482a422aec97e.png)
![微信截图_20181202200916.png](https://static.studygolang.com/181202/18bda8faddde9d493c0b52ec03cce283.png)
![微信截图_20181202200810.png](https://static.studygolang.com/181202/32a4f808a047eca3efa278042f412509.png)
请大神解惑 不胜感激
这个不是内存溢出,这里是有空对象
你这个提示实在看不清位置。
但至少有一个地方会有问题的。
defer res.Body.Close()
就是必须先判断err,再defer close。
不然真的出错的话,相应对象(res)是空的,会报这个错误。
#1
更多评论
在yujiahaol68的文章https://studygolang.com/articles/12319?fr=sidebar的#2中找到相似问题代码.修改运行中等待结果
#2