Golang panic:runtime error:invalid memory address or nil pointer dereference [

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.