簡體   English   中英

如何在go列表中添加“子”結構

[英]How to append a “sub”struct to a list in go

type A struct {
  B []struct {
    C string
    D []struct {
      E string
      F []struct {
        G string
      }
    }
  }
}

可以說我有一個結構A的實例,我想在其中附加一個結構D。 我會嘗試做類似的事情

var a A;
...
a.B.D = append(a.B.D, ???)

??? = ABD

->類型A沒有方法B

??? = D

->未定義:D

-編輯更完整的例子-

type A struct {
  B []struct {
    C string
    D hugeNestedElement
  }
}

var a A
// Goal is to create many B's
a = append(a, what_goes_here)
// or ...
a = append(a.B, what_goes_here)

由於B是結構的一部分,因此它直接沒有D屬性。 可能有可變數量的B

type A struct {
  B []struct {
    C string
    D []struct {
      E string
      F []struct {
        G string
      }
    }
  }
}

由於B是切片,因此以下條目假定您要將新的D附加到第一個B

A.B[0].D = append(A.B[0].D, anotherD)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM