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

WBOY
Release: 2024-02-08 21:50:21
forward
787 people have browsed it

在 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();
}
Copy after login

Because I understand

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

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template