Casting table.Row to My Own Implementation #392
Unanswered
joshdorsey
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was trying to add some form of user data to the Table widget's rows, because otherwise to identify a row I have to put a string in the table.Row (aka []string) object and display it to the user. I tried adding an extra string to the slice for each row, thinking that the number of columns should bound what row elements are accessed and it would be safely ignored, but it panics on an index out of bounds, so that doesn't work.
I noticed a comment on the
SelectedRow()
function that seems to give a hint on how to do this, but I'm not sure. It says you can cast the result ofSelectedRow()
"to your own implementation" here. I tried making a struct that embeds a[]string
, and casting my[]MyRow
to a[]table.Row
, but that's not a valid conversion (because the memory layouts are different, I assume). I think becausetable.Row
is a type alias to[]string
, and not an interface type, there's nothing I actually could put into the table that I would then be able to cast to something else on the way out, but maybe I'm missing something about Go's type system.So it seems the only way to put user data in the table is to convert it to a string, display it to the user, and parse back to whatever other type when I need to inspect it. Or a parallel array in my own code, and use
Cursor()
to correspond the two, but this isn't mentioned anywhere and seems error-prone. I would think if that were the way to do it the table could just accept my user data as anany
and I could provide a conversion to[]string
for it. Any recommended other way to put user data on each table row?I have looked through everything in the issues and discussion section mentioning the word table and no one else seems to have this problem, I could've missed one though.
Beta Was this translation helpful? Give feedback.
All reactions