具有动态格式字符串且没有其他参数的 printf 样式函数应使用打印样式函数我的 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 { // 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())}我怎样才能停止这个错误?有人可以向我解释为什么我的 VScode 会在屏幕上显示它吗?请注意,我的代码运行良好。
1 回答

千万里不及你
TA贡献1784条经验 获得超9个赞
fmt.Fprintf()
需要一个格式字符串,其中可能包含将用参数替换的动词。如果您不传递参数,则暗示您可能没有 / 使用格式字符串,因此您不应该fmt.Fprintf()
首先使用。
要将参数写入io.Writer
,请使用fmt.Fprint()
.
fmt.Fprint(w, prettyJSON.String())
来自 vet 的警告是有道理的,因为格式字符串可能不会按原样输出:
fmt.Print("%%\n") fmt.Printf("%%\n")
以上打印(在Go Playground上尝试):
%%%
The%
是格式字符串中的特殊字符,要发出(输出)单个%
符号,您必须%
在格式字符串中使用 double。这只是为了证明这一点,还有其他区别。
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报
0/150
提交
取消