Is there a way to check for common errors without invading the code?

1. Check uninitialized map

For example,

var myMap map[int]int

Obviously, there will be a panic, but I can’t find a way to auto-detect the problem. I have tried some tools like go-vet and golangci-lint. But they don’t work.

2. Check SQL spell

Furthermore, I also encountered another problem.

sqlStr := fmt.Sprintf("INSERT INTO user_table (id, region, status) VALUES (?, ?, ?)")

Here I want to insert three fields into the “user_table” table, so there are three “‘?’” as placeholders after it. The problem is, sometimes I need to insert ten or even more fields. And I will also frequently modify this statement. This will result in changes in the quantity of ‘?’, which may be inconsistent with the field quantity.
Is there a method that can help me automatically check for SQL spelling issues in my code.

If anyone could give me any advice, I would be very grateful

  1. If golangci-lint doesn’t include a linter that checks that, I doubt there is something available… Unless its a “disabled by default” check. I never really understood the golang linting eco system with all those mini-linters and then the various “meta” linters which try to collect and wrap those into a single linter… But golangci-lint already has quite a huge linting database according to my knowledge and will probably incldue any relevant linter.
  2. Linting or checking another language within an untagged string is always problematic in any language. Instead of writing raw SQL queries in a string, you could use a query builder. I have not tried any in Go though, as I never needed to connect to a database using Go after the project where I had to use the RAW queries as provided by the customer…

Thank you for your suggestion.
I carefully considered these two scenarios. Static inspection is indeed too difficult to detect these issues. I should still consider using test cases as much as possible to cover this code. These issues can be easily discovered at runtime.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.