LoginSignup
0
0

More than 3 years have passed since last update.

Goでの構造体埋め込みと、埋め込んだ構造体に構造体を代入する

Posted at

構造体の埋め込みについてはよく書いてあるのだけど
埋め込んだ構造体に、埋め込んだ元の構造体を代入する方法があまりなかったのでメモ

埋め込み

    type A struct {
        A string
    }
    type B struct {
        B string
    }

    type C struct {
        *A
        *B
    }

代入

    a := new(A)
    b := new(B)
    c := new(C)

    a.A = "hoge"
    b.B = "hage"

    c = &C{B: b} // CにBを代入

代入済みのCにAを追加

    c = &C{
        A: a,
        B: c.B,
    }
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