dongque8332 2018-07-07 02:33
浏览 39

golang sql指针值不断重复

Below is a golang function which is being called with an input channel

func getOptions(inChannel <-chan Param) <-chan ParamOptions {
    paramOptions := make(chan ParamOptions )

    go func() {
        defer close(paramOptions )
        var wg sync.WaitGroup

        conn, err := sql.Open("mssql", wellConnStr)
        if err != nil {
            log.Fatal("open connection failed:", err.Error())
        }

        defer conn.Close()

        getParamOptions := func(db *sql.DB, param *Param) {
            defer wg.Done()

            fmt.Println("querying options for ", param.Code, param.Name)

            rows, err := db.Query(`select *
                from dbo.ParamOptions where code=? and name=?`, &param.Code, &param.Name)

            fmt.Println("results for ", param.Name, param.Code)

            if err != nil {
                log.Fatal("query failed:", err.Error())
            }

            defer rows.Close()
            found := false

            ...

                paramOptions <- ParamOptions...

                break
            }

            if found == false {
                fmt.Println("did not find options for ", param.Code, param.Name)
            }
        }

        for paramInChannel := range paramChannel {
            wg.Add(1)
            fmt.Println("retrieving inputs for ", paramInChannel.Code, paramInChannel.Name)
            **go** getParamOptions(conn, &wellInChannel)
        }

        wg.Wait()
    }()

    return paramOptions 
}

If i remove the go keyword before calling the function getParamOptions it works without any problems. However if I use go then the last code and name keeps repeating within the the getParamOptions function, even though the options retrieved seems to be of the correct Param, the Code and Name values are being repeated

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 yolov8边框坐标
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂