我必須重構代碼,但我不知道如何最好地定義我的結構。我有一個米表[]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"`
}
優點:
結構相同,易于使用
缺點:
每個結構實例上丟失了幾個字節
- 1 回答
- 0 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消