When a property or method is called on nil, a null pointer will be reported
Especially the structure pointer, this problem is very easy to occur, the following is the test code
package tools import "fmt" func MyTest() { type MConn struct { Name string } var conn * MConn var conn2 MConn conn3 := new (MConn) conn4 := & MConn{} fmt.Printf("%v,%v,%v,%v" , conn, conn2, conn3, conn4) }
Return separately
<nil>,{},&{},&{}
When a structure pointer variable var conn *MConn is declared, but it is not initialized and the property is called directly, it will appear
panic: runtime error: invalid memory address or nil pointer dereference
Because conn is nil at this time, it is a null pointer
A null operation must be performed, if conn != nil {}
Of course, we sometimes do not make such an obvious error, but when we cooperate with map, this error may occur unintentionally.
var mMap map[ string ]* MConn m1: = mMap[ " name " ] m1.Name = " qqq "
In this code map, when the key element does not exist, the zero value of value is returned, which happens to be *MConn. The zero value is nil, and an error will also be reported.
So the map has to be judged here
var mMap map[ string ]* MConn m1, ok: = mMap[ " name " ] if ok { m1.Name = " qqq " }
Read More:
- [Solved] panic: runtime error: invalid memory address or nil pointer dereference
- [Solved] Docker Startup Error: panic: runtime error: invalid memory address or nil pointer dereference
- Go Slice Error: panic:runtime error:index out of range [0] with length 0 [Solved]
- [Solved] Tsingsee green rhinoceros video pedestrian intelligence detection test error panic: runtime error
- [Solved] electron-builder Package mac Error: panic: runtime error: index out of range
- [Solved] leetcode Common Error: :runtime error: member access within misaligned address 0xbebebebebebebebe for type ‘str
- How to Solve mount error(6): No such device or address
- TensorRT model quantization error: Error Code 1: Cuda Runtime (an illegal memory access was encountered)
- [Solved] runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
- [Solved] – npm run dev Error: listen EADDRINUSE: address already in use :::8000(or 8080 etc.)
- How to Solve golang test Error: # command-line-arguments [command-line-arguments.test]
- How to Solve pod Error: “Authentication token is invalid or unverified. Either verify it with the email that…”
- How to Solve Git error: out of memory
- How to Solve k8s Nodal issues: /sys/fs/cgroup/memory/docker: no space left on device\““: unknown.
- [Solved] panic: proto: duplicate enum registered: raftpb.EntryType
- How to Solve “RIP Address Out Of Range” Error
- It can be opened with localhost, but not with IP address. The request host name is invalid
- Node Memory Overflow: FATAL ERROR: Reached heap limit Allocation failed – JavaScript heap out of memory
- [Solved] Go language gob serialization pointer cannot be addressed Error
- Solve Error: call_and_retry_last allocation failed – javascript heap out of memory