This is a creation in
Article, where the information may have evolved or changed.
Today do message Queue persistent storage service process, Golang operation MySQL Error:
Panic : Listen Runtime Error : Listen Invalid memory address or nil pointer dereference Hear [
After repeated search, the original is a variable definition problem to do a record, and then there are problems to do a reference, the original code:
var listen to db listen to *sql. Dbfunc listen to init () Listen {Listen to listen to the DB, listen to err listen: = Listen to SQL. Open ("MySQL", listen to "root:123456@/test") listen to hear if listen to err hear! = Listen to nil listen to listen to hear panic (err) listen to listen to hear}}/** listen to listen to the record access log listen to */func listen to (c listen to Commonlog) Listen to Saveviewlog () listen to listen to Sqlin listen to: = Listen to the FMT. Sprintf ("Insert listen to into listen to Q_orgviewlog listen (Viewnum, listen to Orgid, listen to statisticsdate) listen to values (1, hear%d, hear%d)", listen to C.orgid, listen to time. Now (). Unix ()) Listen to listen and listen to err listen: = Listen to Db.exec (Sqlin) listen to hear if listen to err hear! = Listen to nil Listen {Listen and listen to the FMT listen and hear. PRINTLN ("User SQL error, execution time:", listen on.) Now (). String (), listen to "Error content:" And listen to err. Error (), Listen to "SQL:", listen to sqlupdate listen to listen and listen to the FMT. PRINTLN (ERR) listen and listen to listen to else listen to listen to the "Listen and hear". PRINTLN ("Update successful") Listen and Hear}}
And then you reported the mistake.
After repeated queries, theDB variable defines the problem: = only valid within the current method , and is modified as follows:
var listen to db listen to *sql. Dbfunc listen to init () Listen {Listen and listen to the Var listen to err listen to listen to hear db, listen to err listen = listen to SQL. Open ("MySQL", listen to "root:123456@/test") listen to hear if listen to err hear! = Listen to nil listen to listen to hear panic (err) listen to listen to hear}}/** listen to listen to the record access log listen to */func listen to (c listen to Commonlog) Listen to Saveviewlog () listen to listen to Sqlin listen to: = Listen to the FMT. Sprintf ("Insert listen to into listen to Q_orgviewlog listen (Viewnum, listen to Orgid, listen to statisticsdate) listen to values (1, hear%d, hear%d)", listen to C.orgid, listen to time. Now (). Unix ()) Listen to listen and listen to err listen: = Listen to Db.exec (Sqlin) listen to hear if listen to err hear! = Listen to nil Listen {Listen and listen to the FMT listen and hear. PRINTLN ("User SQL error, execution time:", listen on.) Now (). String (), listen to "Error content:" And listen to err. Error (), Listen to "SQL:", listen to sqlupdate listen to listen and listen to the FMT. PRINTLN (ERR) listen and listen to listen to else listen to listen to the "Listen and hear". PRINTLN ("Update successful") Listen and Hear}}
Successful execution
This article is from the "Big Hu Study Notes" blog, please be sure to keep this source http://zhixinhu.blog.51cto.com/3132199/1844734