What does the underscore symbol before an import mean in Go?
Hello! I started learning Go and came across a construct I don't understand. In someone else's code, I saw this import:
```go
import (
"fmt"
_ "image/png"
"os"
)
```
I'm confused by the underscore (`_`) before the `"image/png"` string. I know that in Go, unused imports cause a compilation error, but this is clearly something different.
I tried removing this import, and the code stopped working — the program couldn't decode PNG images. However, I'm not calling any functions from the `image/png` package directly. How does this work? Why is such an import needed if we don't explicitly use functions from the package?
In the documentation, I found a mention of `init()` functions, but I didn't fully understand how this relates. Please explain what the underscore before an import is used for, and in
No answers yet.
Log in to leave an answer