Home  >  Article  >  Backend Development  >  Could I get a compilation error with a potential null ptr dereference in Go?

Could I get a compilation error with a potential null ptr dereference in Go?

WBOY
WBOYforward
2024-02-08 21:50:21784browse

在 Go 中,我可能会遇到潜在的 null ptr 取消引用的编译错误吗?

In the Go language, there will be no potential null pointer dereference compilation errors. This is because the Go language avoids the problem of null pointers by design. When you declare a variable, it is initialized to a zero value by default, not a null pointer. At the same time, the Go language also provides a rich error handling mechanism, such as using multiple return values ​​to return error information, and using defer and panic/recover to handle exceptions. These features enable the Go language to catch potential null pointer errors during the compilation phase and provide a safer and more reliable programming environment. Therefore, in Go language, there will be no compilation errors similar to "null ptr dereference".

Question content

A colleague dereferenced a null ptr in go.

I suggested he do what I do - when working in other languages, I tend to turn the warning level up and turn warnings into errors. For example c#, this means I cannot compile this code:

private static string bad_deref(object? object)
{
    return object.tostring();
}

Because I understand

x.cs(y,z): [CS8602] Dereference of a possibly null reference.

To be clear, this is pretty good.

But he told me that go doesn't have a warning level to start with, and by googling it, it seems he was right.

So how to solve this problem in go? Are we back to the old C advice "good programmers don't make mistakes" and if you don't work with perfect people, sometimes a null ptr dereference will happen?

Workaround

This is correct: you cannot cause a compiler error by dereferencing a pointer without handling the nil case. That's because the language specification allows it. There's nothing you can do about it.

You can write wrong/incorrect code in any language, the way to catch errors early or mitigate them is to write tests in almost any language.

Go has a good testing framework built into the go tool itself. You should definitely take advantage of it. Write tests on the go. Go tests should be executed regularly, they run automatically during CI/CD and before releases. Most IDEs will also run saved tests automatically.

Another trick you can use: If nil pointers don't make sense, write an API that uses non-pointer types. Of course this has its drawbacks (sometimes pointers are necessary); if using a pointer makes more sense, use it, but check for nil values, and handle them properly (e.g. return error, Panic if that makes sense, etc.). Also write tests to test with nil pointer values.

The above is the detailed content of Could I get a compilation error with a potential null ptr dereference in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete