Golang 犯错集合

type * string does not support indexing

代码

package main

import (
    "fmt"
    "errors"
)

func main() {
    s := []string{}
    s = append(s, "hello")
    s = append(s, "world")
    s = append(s, "ailuoy")

    Pop(&s)
    Pop(&s)
    str, _ := Pop(&s)
    fmt.Println(str)
    fmt.Println(s)
}

//从slice中pop最新插入的一条记录
func Pop(s *[]string) (str string, err error) {
    length := len(*s)
    if length == 0 {
        err = errors.New("元素不够了")
        return
    }
    for i := length - 1; i <= length; i++ {
        // 这里有问题
        str = *s[i]
        // 改成这样
        str = (*s)[i]
        remove(s, i)
        break
    }
    return
}

//remove slice
func remove(s *[]string, i int) {
    if len(*s) == 0{
        panic("空...")
    }
    *s = append((*s)[:i], (*s)[i+1:]...)
    return
}

纠正

str = (*s)[i]//纠正

参考文章

undefined: unix.IoctlGetTermios

go get github.com/sirupsen/logrus
# golang.org/x/crypto/ssh/terminal
../../../golang.org/x/crypto/ssh/terminal/util.go:30:12: undefined: unix.IoctlGetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:38:18: undefined: unix.IoctlGetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:54:12: undefined: unix.IoctlSetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:64:18: undefined: unix.IoctlGetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:75:9: undefined: unix.IoctlSetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:80:13: undefined: unix.IoctlGetWinsize
../../../golang.org/x/crypto/ssh/terminal/util.go:98:18: undefined: unix.IoctlGetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:107:12: undefined: unix.IoctlSetTermios
../../../golang.org/x/crypto/ssh/terminal/util.go:111:8: undefined: unix.IoctlSetTermios

不妨试着更新下go的版本 或者重新装一下.. 我是从1.10.2=>1.11.2

如果还是报错.. 查看下你的命令行下的 go version 确实是不是已经切换到 1.11.2 ,如果不是.重启命令行

defer如何接收返回值

code

func FindHouseCount(url string) (findCount int) {
    resp, _ := fetcher.GetByProxy(url)
    //崩是崩到这行的 resp.Body.Close()
    defer resp.Body.Close()
    doc, _ := goquery.NewDocumentFromReader(resp.Body)
    convFindCount, _ := strconv.Atoi(strings.TrimSpace(doc.Find(".resblock-have-find").Find(".value").Text()))
    findCount = convFindCount
    return
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x123b6ec]

goroutine 1 [running]:
spider-hourse/lianjia/parser.FindHouseCount(0xc000222a80, 0x35, 0x0)
    /Users/ailuoy/go-work/goproject/src/spider-hourse/lianjia/parser/cityList.go:60 +0x4c
spider-hourse/lianjia/parser.ParseCityList(0xc00054e000, 0x5608f, 0x7fe00, 0xc000180000, 0x28, 0x40, 0xc000492800, 0x28, 0x40)
    /Users/ailuoy/go-work/goproject/src/spider-hourse/lianjia/parser/cityList.go:24 +0x623
spider-hourse/engine.SimpleEngine.worker(0x12d9a0e, 0x22, 0x12e5990, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc000155ea0, ...)
    /Users/ailuoy/go-work/goproject/src/spider-hourse/engine/engine.go:44 +0x1fa
spider-hourse/engine.SimpleEngine.Run(0xc0000d7f70, 0x1, 0x1)
    /Users/ailuoy/go-work/goproject/src/spider-hourse/engine/engine.go:17 +0x107
main.main()
    /Users/ailuoy/go-work/goproject/src/spider-hourse/main.go:9 +0x6f
exit status 2

解决方案1

对resp进行判断,因为这里resp是一个nil

if resp == nil{
    return 
}

解决方案2

defer 一个闭包

defer func() {
    err := resp.Body.Close()
    fmt.Println(err)
}()

最终解决方案

resp, err := fetcher.GetByProxy(url)
if err != nil {
    fmt.Println("caller FindHouseCount err: ", err)
    return
}

mongo read tcp 192.168.5.4:62964->192.168.5.4:27017: i/o timeout"

read tcp 192.168.5.4:62964->192.168.5.4:27017: i/o timeout"

所有账号密码都正确..就是用mongo查询数据的时候报这个错误

查看mongo日志
docker logs --tail 10 -f mongo

最终发现是由于没有上索引导致了查询时间慢..超过了超时时间..

panic: sync: WaitGroup is reused before previous Wait has returned

这是在一个接口中暴露出来的..这个接口 用 sync.WaitGroup{} 开携程的调用了一个包中的方法

原因是 还没有调用add()方法就调用了wait()

之前代码

package tool

var(
    fpCompareWg = sync.WaitGroup{}
)

func compare(){
    .....
    for _, offset := range box {
        fpCompareWg.Add(1)
        go func(selfOffset int) {
            rate, _ := crossCorrelation(source, target, selfOffset)
            corrXy = append(corrXy, rate)
            fpCompareWg.Done()
        }(offset)
    }
    fpCompareWg.Wait()
    return
}

这里我是把fpCompareWg作为一个全局变量在使用

修改之后代码

这里的 fpCompareWg 只能作为一个局部变量使用

package tool
func compare(){
    var(
        fpCompareWg = sync.WaitGroup{}
    )
    .....
    for _, offset := range box {
        fpCompareWg.Add(1)
        go func(selfOffset int) {
            rate, _ := crossCorrelation(source, target, selfOffset)
            corrXy = append(corrXy, rate)
            fpCompareWg.Done()
        }(offset)
    }
    fpCompareWg.Wait()
    return
}