encoding/asn1 I can't Marshal a struct with pointer members
149 views
Skip to first unread message
J Liu
unread,
Apr 27, 2024, 9:03:39 PMApr 27
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
My program is like this:
type Girl struct { Name string Age int }
type Person struct { girl *Girl
job string }
What should I do to Marshal 'Person'?
Jan Mercl
unread,
Apr 27, 2024, 9:27:42 PMApr 27
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to J Liu, golang-nuts
I think you need to export the fields, the same way like the Girl struct have them exported, ie. by making the first character of the names upper case.
hth
J Liu
unread,
Apr 28, 2024, 12:40:17 PMApr 28
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
I understand this problem. My real program uses export correctly, but the problem I have is not export, asn1: structure error: unknown Go type: *pkg.Girl
Def Ceb
unread,
Apr 28, 2024, 1:01:18 PMApr 28
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
Most marshal/unmarshal functions are unwilling to marshal/unmarshal
structs with pointer fields. Changing them to direct values fixes this.
Example:
https://go.dev/play/p/ykmpBm0bXqn
I do not think there is any other simple alternative.
J Liu:
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
Thank you very much for your answer, but I cannot modify the definition of the field because this is the structure of the third-party library.
Jason Phillips
unread,
Apr 29, 2024, 5:14:52 PMApr 29
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
Then that type probably won't work with encoding/asn1.
If you have control over the call to asn1.Marshal, you could always create a "data transfer object" to translate between this third-party type and a type that you control that has a structure compatible with encoding/asn1.