What does "printf-style function with dynamic format string and no further arguments should use print-style function instead" mean?

huangapple go评论86阅读模式
英文:

What does "printf-style function with dynamic format string and no further arguments should use print-style function instead" mean?

问题

>printf-style函数具有动态格式字符串且没有其他参数时,应改用print-style函数

我的VScode一直在突出显示我的fmt.Fprintf(w, prettyJSON.String())语句,并显示上述警告。不确定它的含义,也不知道如何修复。以下是我使用Fprintf()的示例:

func (s *Server) getWorkSpaces(w http.ResponseWriter, r *http.Request) {
	client := &http.Client{}
	var prettyJSON bytes.Buffer
	req, err := http.NewRequest("GET", "url.com", nil)
	if err != nil {
		// 如果解析请求时出错,向用户返回错误并退出函数
		responses.ERROR(w, http.StatusBadRequest, fmt.Errorf("无法读取请求体:%v", err))
		return
	}


	resp, err := client.Do(req)
	if err != nil {
		// 如果解析请求时出错,向用户返回错误并退出函数
		responses.ERROR(w, http.StatusBadRequest, fmt.Errorf("无法读取请求体:%v", err))
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}
	error := json.Indent(&prettyJSON, body, "", "\t")
	if error != nil {
		log.Println("JSON解析错误:", error)
		return
	}

	fmt.Fprintf(w, prettyJSON.String())
}

我该如何停止这个错误?有人能解释一下为什么我的VScode会在屏幕上显示这个错误吗?请注意,我的代码运行良好。

英文:

>printf-style function with dynamic format string and no further arguments should use print-style function instead

My VScode keeps highlighting my fmt.Fprintf(w, prettyJSON.String()) statements with the warning stated above. Not sure what it means, or how to fix. Here is an example on how I use Fprintf():

func (s *Server) getWorkSpaces(w http.ResponseWriter, r *http.Request) {
	client := &http.Client{}
	var prettyJSON bytes.Buffer
	req, err := http.NewRequest("GET", "url.com", nil)
	if err != nil {
		// if there was an error parsing the request, return an error to user and quit function
		responses.ERROR(w, http.StatusBadRequest, fmt.Errorf("unable to read request body: %v", err))
		return
	}


	resp, err := client.Do(req)
	if err != nil {
		// if there was an error parsing the request, return an error to user and quit function
		responses.ERROR(w, http.StatusBadRequest, fmt.Errorf("unable to read request body: %v", err))
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}
	error := json.Indent(&prettyJSON, body, "", "\t")
	if error != nil {
		log.Println("JSON parse error: ", error)
		return
	}

	fmt.Fprintf(w, prettyJSON.String())
}

How can I stop this error? Could someone explain to me why my VScode is throwing that up on the screen? Note that my code runs and works well.

答案1

得分: 9

fmt.Fprintf() 期望一个包含占位符的格式字符串,这些占位符将被参数替换。如果你不传递参数,那就意味着你可能没有或者不使用格式字符串,所以你不应该首先使用 fmt.Fprintf()

如果只是将参数写入到一个 io.Writer 中,可以使用 fmt.Fprint()

fmt.Fprint(w, prettyJSON.String())

这个警告是合理的,因为格式字符串可能不会按原样输出:

fmt.Print("%%\n")
fmt.Printf("%%\n")

上面的代码输出结果如下(在 Go Playground 上尝试一下):

%%
%

在格式字符串中,% 是一个特殊字符,如果要输出一个单独的 % 符号,必须在格式字符串中使用两个 %。这只是为了证明这一点,还有其他的区别。

参考相关问题:

https://stackoverflow.com/questions/58076692/no-placeholders-in-format-string/58076735#58076735

https://stackoverflow.com/questions/11123865/format-a-go-string-without-printing/31742265#31742265

英文:

fmt.Fprintf() expects a format string that may contain verbs that will be substituted with arguments. If you do not pass arguments, that hints you're likely not having / using a format string, so you shouldn't use fmt.Fprintf() in the first place.

To just write arguments into an io.Writer, use fmt.Fprint().

fmt.Fprint(w, prettyJSON.String())

The warning is more than justified from vet, because the format string may not get outputted as-is:

fmt.Print("%%\n")
fmt.Printf("%%\n")

The above prints (try it on the Go Playground):

%%
%

The % is a special character in format strings, and to emit (output) a single % sign you have to use double % in the format string. This is just to prove the point, there are other differences.

See related questions:

https://stackoverflow.com/questions/58076692/no-placeholders-in-format-string/58076735#58076735

https://stackoverflow.com/questions/11123865/format-a-go-string-without-printing/31742265#31742265

huangapple
  • 本文由 发表于 2022年2月9日 23:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/71052339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定