具有动态格式字符串且没有其他参数的 printf 样式函数应使用 print 样式函数
我的 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())
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能停止这个错误?有人可以向我解释一下为什么我的 VScode 将其显示在屏幕上吗?请注意,我的代码运行良好。
icz*_*cza 10
fmt.Fprintf()需要一个格式字符串,其中可能包含将被参数替换的动词。如果您不传递参数,则暗示您可能没有/使用格式字符串,因此您不应该fmt.Fprintf()首先使用。
要将参数写入io.Writer,请使用fmt.Fprint().
fmt.Fprint(w, prettyJSON.String())
Run Code Online (Sandbox Code Playgroud)
兽医的警告是完全合理的,因为格式字符串可能不会按原样输出:
fmt.Print("%%\n")
fmt.Printf("%%\n")
Run Code Online (Sandbox Code Playgroud)
上面的打印结果(在Go Playground上尝试一下):
%%
%
Run Code Online (Sandbox Code Playgroud)
是%格式字符串中的特殊字符,要发出(输出)单个%符号,您必须%在格式字符串中使用 double 。这只是为了证明这一点,还有其他差异。
查看相关问题:
| 归档时间: |
|
| 查看次数: |
5861 次 |
| 最近记录: |