具有動態格式字符串且沒有其他參數的 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")
以上打?。ㄔ贕o Playground上嘗試):
%%%
The%
是格式字符串中的特殊字符,要發出(輸出)單個%
符號,您必須%
在格式字符串中使用 double。這只是為了證明這一點,還有其他區別。
- 1 回答
- 0 關注
- 101 瀏覽
添加回答
舉報
0/150
提交
取消