Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:go-best-practices/use-fprintf-when-possible
Language: Go
Severity: Warning
Category: Best Practices
Description
You do not need to convert the string into a slice of bytes to use Write, you can just use the string directly.
Non-Compliant Code Examples
packagemainimport("fmt""os")funcmain(w,ax){r,w:=io.Pipe()gofunc(){idx,name:="4th","Mars"w.Write([]byte(fmt.Sprintf("The %s planet is %s.\n",idx,name)))w.Write([]byte(fmt.Sprint("The ",idx," planet is ",name,".\n")))w.Write([]byte(fmt.Sprintln("The",idx,"planet is",name,".")))w.Close()}()if_,err:=io.Copy(os.Stdout,r);err!=nil{log.Fatal(err)}}
Compliant Code Examples
packagemainimport("fmt""os")funcmain(){constidx,name="4th","Mars"n,err:=fmt.Fprint(os.Stdout,"The ",idx," planet is ",name,".\n")iferr!=nil{fmt.Fprintf(os.Stderr,"Fprint: %v\n",err)}}
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