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)}}
シームレスな統合。 Datadog Code Security をお試しください
Datadog Code Security
このルールを試し、Datadog Code Security でコードを解析する
このルールの使用方法
1
2
rulesets:- go-best-practices # Rules to enforce Go best practices.