Note that there are some explanatory texts on larger screens.

plurals
  1. POGet static type of struct element
    primarykey
    data
    text
    <p>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 <code>syscall.Stat_t.Ino</code>, which has type <code>uint64</code> on my machine, to ino_entry, defined as</p> <pre><code>type ino_entry struct { st *syscall.Stat_t nodes []string } </code></pre> <p>where nodes represents the names of all the files hard linked to the inode.</p> <p>However, I want to avoid putting the literal type <code>uint64</code> in the specification of the map if I can. That is, instead of writing <code>map [uint64] ino_entry</code>, I'd prefer to write <code>map [typeof(syscall.Stat_t.Ino)] ino_entry</code>, where <code>typeof(x)</code> returns the static (i.e., compile-time) type of <code>x</code>. I've tried <code>map [syscall.Stat_t.Ino] ino_entry</code> and, by analogy with type switches, <code>map [syscall.Stat_t.Ino.(type)] ino_entry</code>, but both of these are syntax errors. Needless to say, <code>map [reflect.Typeof(syscall.Stat_t.Ino)] ino_entry</code> 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 <code>inomap := make(map [interface{}] ino_entry)</code> and then access the elements using type assertions (I don't know the correct syntax for this though).</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload