0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

go1.24に上げたらnon-constant format string in call toがでた

Posted at

エラー内容

Goで1.21から1.24にあげようとした時に下のようなエラーが出るようになりました。

Error: ./main.go:98:11: non-constant format string in call to (****).Infof

原因

エラーが出ていた箇所は以下のような感じにに書かれていました。

fmt.Printf(`test-{data}`)

TypeScript等でよくみる変数を文字列として入れて表示する処理です。

この部分でエラーが起きていました。

解決方法

本来printfは下のようにフォーマット修飾子とセットで使います。

fmt.Printf("test-%v",data)

Go1.24からはPrintfでgo vetを使った静的解析が強化されており、エラーのように文字列に変数を入れ込む形ではなく、第二引数に変数を入れたり、フォーマット修飾子で表さないといけないようになりました。

https://arc.net/l/quote/skcwyjvy

ある意味複数の書き方ができるとメソッドの利用用途がわからなくなるので、初学者にとってはありがたいかもしれないなと感じました。

go vetとは

静的解析パッケージで、バグが起きそうなところをコードを解析して指摘してくれます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?