Junior — Middle
What methods and tools did you use to verify the correctness and quality of your code?
sobes.tech AI
Answer from AI
To verify the correctness and quality of Go code, the following methods and tools are usually used:
- Testing: writing unit tests using the
testingpackage. It allows testing individual functions and methods. - Test coverage: the command
go test -covershows what percentage of the code is covered by tests. - Static analysis:
golintfor style checking,go vetfor identifying potential errors. - Formatting:
gofmtautomatically formats code according to Go standards. - Profiling and debugging:
pproffor performance analysis.
Example of a simple test:
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}