The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Reid

为什么这里会报 ineffective assignment to field DB.Sess (SA4005)go-staticcheck

  •  
  •   Reid · Mar 1, 2022 · 2800 views
    This topic created in 1539 days ago, the information mentioned may be changed or developed.

    Go 语言 下面这段代码会提示这个 warning 。不太理解为啥? 逻辑是从 slice 中取一个 session 执行,执行后放回 slice ,有更好用的方式吗?

    func (d DB) Run(stmt string) {
    	if len(d.Sess) > 0 {
    		sess := d.Sess[0]
    		d.Sess = d.Sess[1:]
    		result, err := sess.ExecuteJson(stmt)
    		if err != nil {
    			fmt.Println("sess Executejson err: ", err)
    			d.Sess = append(d.Sess, sess)
    			return
    		}
    		fmt.Println(gjson.Get(string(result), "results.0.data"))
    		d.Sess = append(d.Sess, sess)
    	}
    }
    

    codeImage

    2 replies    2022-03-01 15:06:45 +08:00
    MicroPan
        1
    MicroPan  
       Mar 1, 2022
    d 是通过 DB(值) 调用的 Run, 修改 Sess 并不会生效.
    你可以试试改为 func (d *DB) Run(stmt string) 试试是否可以解决这个问题
    Reid
        2
    Reid  
    OP
       Mar 1, 2022
    @MicroPan 是的,是这个问题,指针接受者 和 值接受者的区别...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1667 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 16:33 · PVG 00:33 · LAX 09:33 · JFK 12:33
    ♥ Do have faith in what you're doing.