1 回答

TA貢獻1796條經驗 獲得超4個贊
我已經證實了我的懷疑,現在有了正確的答案。根本問題是我在使用 C++ API 時出錯(在某些情況下這會導致只寫 1/2 的雙精度數),而我實際上是在嘗試在 GO 中重復這個錯誤。其實解決方法很簡單。
傳遞到屬性方法中的屬性類型read不是屬性的類型,而是存儲在內存中時您希望將其轉換為的類型。這意味著我的 C++ 代碼應該簡單得多,因為不需要檢查屬性類型,也不需要檢查static_cast結果。要讀取和存儲屬性值,依靠 HDF5 執行轉換并在屬性不可轉換為雙精度值時拋出合適的異常,非常簡單
const auto att = dataSet.openAttribute("my attribute name");
att.read(H5::PredType::NATIVE_DOUBLE, &attributeValue);
GO 版本的工作量更大,因為我們必須手動管理對象生命周期和錯誤條件,但就在這里。(請注意,我假設“...處理錯誤...”也涉及提前退出,否則需要額外的 if 語句來檢查 att 是否為 nil。)
att, err := dataSet.OpenAttribute("my attribute name")
if err != nil {
...handle the error...
}
err = att.Read(&attributeValue, hdf5.T_NATIVE_DOUBLE)
if err != nil {
...handle the error...
}
err = att.Close()
if err != nil {
...handle the error...
}
- 1 回答
- 0 關注
- 189 瀏覽
添加回答
舉報