Upgrade to Pro — share decks privately, control downloads, hide ads and more …

go generate tips

kyokomi
June 02, 2015

go generate tips

Gunosy.go#13 GolangTipsの発表内容です。
go generateについて

kyokomi

June 02, 2015
Tweet

More Decks by kyokomi

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • Twi%er:)@k_yokomi)ʢ͖ΐ͜Έʣ※1 • GunosyͰGoΤϯδχΞ΍ͬͯ·͢ • Cocos2d9xͰήʔϜ࡞ͬͨΓ • Gunosy.goͷओ࠵ͱ͔΍ͬͯ·͢ •

    GoConͰLT͠·͢)! ※1"ΞΠίϯ͸݄ඣͷᘡᘚ͞ΜͰ͢ɻ @k_yokomi,)2015/06/02)ʮGolangTipsʢGunosy.go#13ʣ)go)generate)?psʯ 2
  2. go#generateͱ͸ • Go$1.4͔Β࢖͑·͢ • ༻్ͱͯ͠͸ɺgo$buildͷલʹ໌ࣔతʹ࣮ߦ͢Δ΋ͷ • go generateͱ͍͏ίϚϯυͰ࣮ߦͰ͖ɺGoͷιʔείʔυ ಺ͷ/ /go:generate$ʙΛಡΈऔ࣮ͬͯߦ͞ΕΔ

    $5$ϑΝΠϧ໊͕os.Getenv("GOFILE")ͰऔΕΔ $5$ύοέʔδ໊͕os.Getenv("GOPACKAGE")ͰऔΕΔ @k_yokomi,)2015/06/02)ʮGolangTipsʢGunosy.go#13ʣ)go)generate)?psʯ 3
  3. go/formatύοέʔδ goͷιʔείʔυΛgenerate͢Δ৔߹͸ɺඞͣgofmt͔͚͓ͯ͘ͱ ࢖͏ਓʹ΋༏͍͠*⭐ import "go/format" bts, err := format.Source(data) if

    err != nil { return fmt.Errorf("go format error: %s \n%s", err, string(data)) } ߏจΤϥʔ΋ݕग़Ͱ͖Δ!❗!❗ @k_yokomi,)2015/06/02)ʮGolangTipsʢGunosy.go#13ʣ)go)generate)?psʯ 7
  4. go#lintͬΆ͍replace kyokomi/lintname,΄΅શ෦ɺgolang/lintͷίϐϖͰ͕͢ɻɻɻ package main import ( "fmt" lint "github.com/kyokomi/lintname" )

    func main() { fmt.Println(lint.LintName("HogeId")) // => HogeID } @k_yokomi,)2015/06/02)ʮGolangTipsʢGunosy.go#13ʣ)go)generate)?psʯ 8
  5. serenize/snaker CamelCase'<=>'SnakeCaseΛม׵͢ΔϥΠϒϥϦɻ package main import ( "fmt" "github.com/serenize/snaker" ) func

    main() { fmt.Println("HogeId => ", snaker.CamelToSnake("HogeId")) // HogeId => hoge_id fmt.Println("hoge_id => ", snaker.SnakeToCamel("hoge_id")) // hoge_id => HogeID } @k_yokomi,)2015/06/02)ʮGolangTipsʢGunosy.go#13ʣ)go)generate)?psʯ 9