I've looked in the Golang documentation and haven't seen an example of how to achieve what I'm looking to do. Specifically, I'm trying to write a map from inodes, represented by syscall.Stat_t.Ino
, which has type uint64
on my machine, to ino_entry, defined as
type ino_entry struct {
st *syscall.Stat_t
nodes []string
}
where nodes represents the names of all the files hard linked to the inode.
However, I want to avoid putting the literal type uint64
in the specification of the map if I can. That is, instead of writing map [uint64] ino_entry
, I'd prefer to write map [typeof(syscall.Stat_t.Ino)] ino_entry
, where typeof(x)
returns the static (i.e., compile-time) type of x
. I've tried map [syscall.Stat_t.Ino] ino_entry
and, by analogy with type switches, map [syscall.Stat_t.Ino.(type)] ino_entry
, but both of these are syntax errors. Needless to say, map [reflect.Typeof(syscall.Stat_t.Ino)] ino_entry
doesn't work. As far as I can tell, the only way to implement this without hardcoding the type of the structure element is to use inomap := make(map [interface{}] ino_entry)
and then access the elements using type assertions (I don't know the correct syntax for this though).
Is there a way to declare the type of a variable based on the static type of another variable, or struct or interface member, without hardcoding that type explicitly?