Skip to content

Instantly share code, notes, and snippets.

Last active October 27, 2020 22:02
What would you like to do?
A simple Go module for reading a single array of 64 bit floating point numbers from a HDF5 formatted file.
package main
import (
// load a scalar-valued variable of type int from an attribute of the given name on the given dataset.
func getIntScalarAttr(dataset *hdf5.Dataset, attrName string) int {
var value int
attr, err := dataset.OpenAttribute(attrName)
if err != nil {
attr.Read(&value, hdf5.T_NATIVE_UINT32)
return value
// load the image data from the indicated HDF5 file.
func getImageDataset(fileName string) *hdf5.Dataset {
file, err := hdf5.OpenFile(fileName, hdf5.F_ACC_RDONLY)
if err != nil {
dataset, err := file.OpenDataset("data")
if err != nil {
return dataset
func main(){
// load dataset & dataset attributes
dataset := getImageDataset("image.h5")
height := getIntScalarAttr(dataset, "height")
width := getIntScalarAttr(dataset, "width")
// create image data slice using height/width attributes
image := make([]float64, height*width)
// reshape and process here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment