Replace errors.New(fmt.Sprintf()) with fmt.Errorf()

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: go-best-practices/use-errorf-when-possible

Language: Go

Severity: Warning

Category: Best Practices

Description

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

package main

import (
	"errors"
	"fmt"
)

func main() {
	fmt.Println(errors.New(fmt.Sprintf("This is an error")))
}

Compliant Code Examples

package main

import (
	"errors"
	"fmt"
)

func main() {
	fmt.Println(fmt.Errorf("This is an error")))
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis