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

構造体初期化の方法について

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 構造体初期化の方法について

Avatar for ryota kise

ryota kise

August 26, 2022
Tweet

More Decks by ryota kise

Other Decks in Programming

Transcript

  1. 構造体の処理化 構造体にオプショナルな値がある場合、初期化時に渡した時はデフォルト値にしてほしい ケースはよくあると思います。 その時の手法として - functional options pattern - builder

    pattern (method chaining) の二つのパターンがあります。 今回はその二つのパターンの説明と どこが優れているのかについて紹介していこうと思います。
  2. 手法を使用しない場合 そもそもこの方法を使うと何が嬉しいのかを先に説明します 例えばクライアント初期化時にconfigが必須でいくつかオプショナルな値が渡せる時 client := s3.NewFromConfig(config, “us-east-1", true) のように愚直に渡すやり方だと -

    常に詳しくオプションの値を設定する必要があること - 拡張するたびに引数が増えることになり、変更された際に後方互換性がない - とりあえず渡す状況になる可能性があること - なんの設定値なのか関数を見に行かないとわからないこと などが問題として挙げられます。 拡張の見込みがなくシンプルなもので良いのであればこの方法でも問題はありませんが 拡張前提であればこのような実装はするべきではありません
  3. functional option pattern (以後FOP) Functional options are a method of

    implementing clean/eloquent APIs in Go. Options implemented as a function set the state of that option. FOPは、Goでクリーン/雄弁なAPIを実装するための方法です。 関数として実装されたオプションは、そのオプションの状態を設定します。 さきほどの例で示した aws-sdk-go でもこのパターンを使用しています。 client := s3.NewFromConfig(config, “us-east-1", true) client := s3.NewFromConfig(config, func(o *s3.Options) { o.Region = “us-east-1” o.DisableSSL = true } )
  4. builder pattern (以後 BP) Builder pattern separates the construction of

    a complex object from its representation so that the same construction process can create different representations. BPは、複雑なオブジェクトの構築とその表現を分離し、同じ構築プロセスで異なる表現を作成できるようにします。 特徴としてはメソッドをチェーンした最後に Buildメソッドを呼び出すことです。 client := s3.NewFromConfig(config, “us-east-1", true) client := s3.NewFromConfig(config) .WithRegion(“us-east-1”) .WithDisableSSl(true) .Build()
  5. Go 1.17/1.18での変更箇所 Go 1.17 implements a new way of passing

    function arguments and results using registers instead of the stack. Benchmarks for a representative set of Go packages and programs show performance improvements of about 5%, and a typical reduction in binary size of about 2%. Go 1.18 expands the supported platforms to include 64-bit ARM (GOARCH=arm64), big- and little-endian 64-bit PowerPC (GOARCH=ppc64, ppc64le), as well as 64-bit x86 architecture (GOARCH=amd64) on all operating systems. Go 1.17 は、スタックの代わりにレジスタを使用して関数の引数と結果を渡す新しい方法を実装しています 。代表 的な Go パッケージとプログラムのベンチマークでは、パフォーマンスが約 5% 向上し、バイナリ サイズが通常約 2% 削減されました。 Go 1.18 はサポートするプラットフォームを拡大し、 64 ビット ARM (GOARCH=arm64) とビッグ/リトルエンディア ンの 64 ビット PowerPC (GOARCH=ppc64, ppc64le) に加え、すべての OS 上の 64 ビット x86 アーキテクチャ (GOARCH=amd64) をサポートしました。
  6. 最後に 今回は構造体初期化の方法として選択肢に上がる functional option pattern と builder pattern について話し ました。

    今回の発表が誰かの助けになれば幸いです。 ご清聴ありがとうございました 🙇
  7. 参考資料 - https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md - https://github.com/tmrts/go-patterns/blob/master/creational/builder.md - https://github.com/knsh14/uber-style-guide-ja/blob/master/guide.md#functional-options - https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html -

    https://www.calhoun.io/using-functional-options-instead-of-method-chaining-in-go/ - https://www.pospome.work/entry/2018/03/12/014109 - https://go.dev/doc/go1.17#compiler - https://tip.golang.org/doc/go1.18#compiler - https://qiita.com/momotaro98/items/51398e728b92261215a5