Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
printf-args-num-ok?
decafe09
February 24, 2018
Programming
0
71
printf-args-num-ok?
goでprintfの引数の数をチェックするときの諸々について。
decafe09
February 24, 2018
Tweet
Share
More Decks by decafe09
See All by decafe09
GoならわかるGIF
decafe09
0
190
よくあるWebサービスのAPIを作ったらいろいろ勉強になった話/WebApplicationAPITips
decafe09
1
270
goaを使ったAPI開発/api-development-with-goa
decafe09
0
1.9k
Other Decks in Programming
See All in Programming
Workshop on Jetpack compose
aldefy
0
140
Unity+C#で学ぶ! メモリレイアウトとvtableのすゝめ 〜動的ポリモーフィズムを実現する仕組み〜
rossam
1
260
社会人 20 年目エンジニア、発信で技術学びなおしてる話
e99h2121
1
140
T3 Stack and TypeScript ecosystem
quramy
3
770
Milestoner
bkuhlmann
1
250
Step Functions Distributed Map を使ってみた
codemountains
0
110
フロントエンドで学んだことをデータ分析で使ってみた話
daichi_igarashi
0
190
Hono v3 - Do Everything, Run Anywhere, But Small, And Faster
yusukebe
4
130
なぜRubyコミュニティにコミットするのか?
luccafort
0
320
Rust、何もわからない...#6発表資料
ryu19
0
130
Listかもしれない
irof
1
280
Becoming an Android Librarian (Android World Wide 2023 Jan)
skydoves
2
220
Featured
See All Featured
A Philosophy of Restraint
colly
193
15k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
15
1.2k
WebSockets: Embracing the real-time Web
robhawkes
58
6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
44
14k
Code Reviewing Like a Champion
maltzj
508
38k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1.1M
How GitHub Uses GitHub to Build GitHub
holman
465
280k
How To Stay Up To Date on Web Technology
chriscoyier
779
250k
Build your cross-platform service in a week with App Engine
jlugia
221
17k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
270
12k
Music & Morning Musume
bryan
37
4.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
240
11k
Transcript
printf - args num ok? Umeda.go #3 2018 - 02
- 24 @decafe09
fmt.Printf fmt.Printf("%s", "moji")
fmt.Printf fmt.Printf("%s", "moji", "yobun")
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここで気づける moji%!(EXTRA string=yobun)
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここで気づける moji%!(EXTRA string=yobun) func main() { http.HandleFunc( "/xxx", func(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s", "moji", "yobun") return }, ) http.ListenAndServe(":8080", nil) }
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここでは気づけない $ curl localhost:8080/xxx # ここで気づける moji%!(EXTRA string=yobun)
go vet $ go vet ./main.go:6: Printf call needs 1
arg but has 2 args go vet ! curl " go vet CI go vet
go vet –printfuncs $ go vet –printfuncs=Infof ./main.go:6: Infof call
needs 1 arg but has 2 args func Infof(format string, a ...interface{}) (n int, err error) { return fmt.Printf(format, a...) }
go vet @ Go 1.10 $ go test ./main.go:6: Printf
call needs 1 arg but has 2 args go1.9.3 main.go:6: wrong number of args for format in Printf call: 1 needed but 2 args go1.10 main.go:6: Printf call needs 1 arg but has 2 args go test go vet go test -printfuncs
Humm... format := "%s" fmt.Printf(format, "moji", ”yobun")
Humm... $ go build # ここでは気づけない $ go vet #
ここでは気づけない $ go run main.go # ここで気づける moji%!(EXTRA string=yobun) http ! % # &$"%
Background API6=3.;?>/2,BW!:20/)O APIVK" C %!8>?4+?5JSAPIRFP
[email protected]
YYNTLIEH XXYYEH
XX, YY O(&'!format!O(& %D 71? +<:20/GQ&72-/M%A format#xx,yy*9=-.;?U$
Thinking... "0(' ! 0(',1#%$ /0( $ !
+.)*format !format !&-...
Result func check(format string) error 1. format %! 2. format
message 3. format %! 4. %! func main() { format := "%s" err := check(format) if err != nil { panic(err) } fmt.Println(message(format)) } func message(format string) string { return fmt.Sprintf(format, "moji", "yobun") } func check(format string) error { pre := strings.Count(format, "%!") msg := message(format) post := strings.Count(msg, "%!") if post > pre { return errors.New("format error: " + msg) } return nil }
OK All errors begin with the string "%!" followed sometimes
by a single character (the verb) and end with a parenthesized description. https://golang.org/pkg/fmt/
Check $ go run main.go panic: format error: moji%!(EXTRA string=yobun)
Profile decafe09