Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
printf-args-num-ok?
Search
decafe09
February 24, 2018
Programming
0
130
printf-args-num-ok?
goでprintfの引数の数をチェックするときの諸々について。
decafe09
February 24, 2018
Tweet
Share
More Decks by decafe09
See All by decafe09
GoならわかるGIF
decafe09
0
270
よくあるWebサービスのAPIを作ったらいろいろ勉強になった話/WebApplicationAPITips
decafe09
1
300
goaを使ったAPI開発/api-development-with-goa
decafe09
0
2k
Other Decks in Programming
See All in Programming
How to use Macrobenchmark
veronikapj
0
160
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
15
8.5k
TiDB Serverless ~理想のServerless DBを考える~
soso_15315
1
160
Temporalを取り巻く仕様を整理する
sajikix
0
110
SRE チーム立ち上げ前に考えたこと・取り組んだこと / Considerations and Preparations Before Establishing an SRE Team
mackey0225
3
320
Composing an API the *right* way (Droidcon Berlin 2024)
zsmb
1
450
feature環境をGitHub ActionsとCloudFormationでいい感じに管理する
nealle
2
310
はしめてのプログラミングとロボット制御
watawatavoltage
0
290
Android開発者のための Kotlin Multiplatform入門
ntaro
0
190
Introduction of Happy Eyeballs Version 2 (RFC8305) to the Socket library
coe401_
1
220
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
Architectures with Lightweight Stores: New Rules and Options
manfredsteyer
PRO
0
100
Featured
See All Featured
Web Components: a chance to create the future
zenorocha
307
41k
What's in a price? How to price your products and services
michaelherold
239
11k
It's Worth the Effort
3n
181
27k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.5k
From Idea to $5000 a Month in 5 Months
shpigford
377
46k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
23
1.9k
Documentation Writing (for coders)
carmenintech
63
4.2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
26
1.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
90
47k
StorybookのUI Testing Handbookを読んだ
zakiyama
15
4.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
78
15k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.6k
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 XXNTL@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