亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何組織相似的結構以避免重復而不犧牲內存

如何組織相似的結構以避免重復而不犧牲內存

Go
Smart貓小萌 2023-08-07 16:44:35
我必須重構代碼,但我不知道如何最好地定義我的結構。我有一個米表[]Meter和Metertype Meter struct {    ID          string    `json:"meter_id"`    ConsoProd   string    `json:"conso_prod"`    OperationID string    `json:"op_id"`    Measures    []Measure `json :"measures"`}第 3 個字段將始終位于此處,但“測量”有多種變體,具體取決于過程中的步驟。這是一般Measure結構:// Measure is a measure from a Meter    type Measure struct {        IndexName     string    `json:"index_name"`        IndexValue    int       `json:"index_value"`        Timestamp     time.Time `json:"timestamp"`        Delta         float64   `json:"delta"`        Redistributed float64   `json:"redistributed,omitempty"` // We don t need the redistributed field in Raw Measure    }首先,我們得到“原始值”type RawMeasure struct {    IndexName     string    `json:"index_name"`    IndexValue    int       `json:"index_value"`    Timestamp     time.Time `json:"timestamp"`    Delta         float64   `json:"delta"`}然后我們將計算重新分配的值,并將其存儲到字段redistributed中該措施永遠不會重新分配。另外,我有2個數據源。如果數據來自source2,它永遠不會有IndexName / IndexValuetype RawMeasureFromSource2 struct {    Timestamp     time.Time `json:"timestamp"`    Delta         float64   `json:"delta"`}在這里我們可以看到我可以創建幾個結構:(RawMeasure、RawMeasureFromSource2、Measure),然后我應該創建其他 3 米變量。由于我將管理大量數據,因此我需要小心優化內存,但這似乎會使我的代碼變得更加復雜。有沒有辦法既獲得簡單的代碼又優化內存使用?
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

根據評論,每 10 分鐘 1 次測量絕對不是很多。我會為了簡單起見:


type Measure struct {

    IndexName     *string    `json:"index_name,omitempty"`

    IndexValue    *int       `json:"index_value,omitempty"`

    Timestamp      time.Time `json:"timestamp"`

    Delta          float64   `json:"delta"`

    Redistributed *float64   `json:"redistributed,omitempty"`

}

優點:


結構相同,易于使用

缺點:


每個結構實例上丟失了幾個字節


查看完整回答
反對 回復 2023-08-07
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號