When the error variable is declared in the function, panic: invalid memory address is used when errors.New () is used.

when using errors.New (), report panic: runtime error: invalid memory address or nil pointer dereference

func (self *Control) Register(ctx context.Context, in *push.RegisterRequest) (*push.RegisterResponse, error) {
    var err error
    var res = &push.RegisterResponse{
        Code:false,
        Msg:"",
    }
    userInfo, err := service.CheckRegisterParams(ctx, in)
    if err != nil{
    
        res.Msg = err.Error()
        return res, err
    }
 
    if service.IsBlackIp(ctx, in.UserIp){
        res.Msg = ""
        return res, errors.New("")//painc
        return res, err//

    }
    err = service.Register(ctx, in.User, in.DeviceNo, in.Ip, in.Port, userInfo)
    if err != nil{
    
        res.Msg = err.Error()
        return  res, err
    }
    res.Code = true
    res.Msg = "OK"
    return res, nil
}

detailed error information is as follows:

--- FAIL: TestRegiser (0.01s)
    session_test.go:70: fail, rpc error: code = Unknown desc = 
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14730d0]

goroutine 5 [running]:
testing.tRunner.func1(0xc4201923c0)
    /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:742 +0x29d
panic(0x15028e0, 0x18f2c40)
    /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
 session/test.TestRegiser(0xc4201923c0)
    /Users/xxxx/devsoft/go/src/session/test/session_test.go:72 +0x270
testing.tRunner(0xc4201923c0, 0x15d2e78)
    /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
    /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x2e0
Mar.10,2021

found the reason, it's my pot. If grpc returns the variable before error, * push.RegisterResponse, returns nil, by default, and then I try to access the error message in the form of res.Msg in the test. If res is nil, there is invalid memory address or nil pointer dereference's fault

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38036-2c0c0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38036-2c0c0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?