In Go, it is recommended to use fmt.Errorf() instead of errors.New(fmt.Sprintf()) for creating error values. The fmt.Errorf() function provides a more expressive and convenient way to generate error values by allowing the formatting of error messages.
Non-Compliant Code Examples
packagemainimport("errors""fmt")funcmain(){fmt.Println(errors.New(fmt.Sprintf("This is an error")))}
Compliant Code Examples
packagemainimport("errors""fmt")funcmain(){fmt.Println(fmt.Errorf("This is an error")))}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- go-best-practices # Rules to enforce Go best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines