Golang Error - When marshelling data

712 views
Skip to first unread message

User123

unread,
Jun 16, 2016, 5:44:00 AM6/16/16
to golang-nuts
The data  I am trying to marshal contains serialized data. This data is received in http response.

It works perfectly when I try in version 1.4.1/ 1.4.2.

But when I try this to the latest version 1.6 it gives an error: json: unsupported type: <-chan struct {}

Why is it so? 

I am not able to upgrade to the latest version because of this.

Please help!




Dave Cheney

unread,
Jun 16, 2016, 5:51:48 AM6/16/16
to golang-nuts
Hello,

Can you please provide a runnable code sample that shows the problem.

Thanks

Dave

User123

unread,
Jun 16, 2016, 6:38:38 AM6/16/16
to golang-nuts
I cannot provide the full code sincethere are some dependencies.

I did fmt.println on the data that I am trying to marshall and it looks like this

%!(EXTRA main.JobResponseRoot={{92b4f95b309e8db0f8d56afadefc} http.res {[{{  map[] <nil>} "Date","Time","Time_Zone","Source","Name","Raw_Data"
 <nil> 0x10a3c2d0}]}})

User123

unread,
Jun 16, 2016, 7:55:41 AM6/16/16
to golang-nuts
Is it giving error since it has null values? 

I'm just not able to get it. Since this code works perfectly fine in 1.4.2

Jakob Borg

unread,
Jun 16, 2016, 8:01:01 AM6/16/16
to User123, golang-nuts
2016-06-16 11:44 GMT+02:00 User123 <twist...@gmail.com>:
> json: unsupported type: <-chan struct {}

A channel type is not serializable, as the error says. It's possible
that older versions of Go just skipped this without generating an
error, I don't know. If that's the case you may be able to continue
getting that behavior by using a `json:"-"` field tag.

//jb

Konstantin Khomoutov

unread,
Jun 16, 2016, 9:39:27 AM6/16/16
to User123, golang-nuts
On Thu, 16 Jun 2016 04:55:41 -0700 (PDT)
User123 <twist...@gmail.com> wrote:

> Is it giving error since it has null values?
>
> I'm just not able to get it. Since this code works perfectly fine in
> 1.4.2

A wild guess:

1) Your error message is:

json: unsupported type: <-chan struct {}

and indeed the docs of encoding/json clearly state that certain
types cannot be sensibly marshalled and unmarshalled.
Channel types are among them.

2) Some value contained as a field of your value -- which is supposedly
of some custom struct type -- started to contain a variable of type

<-chan struct{}

somewhere between 1.4.2 and 1.6.

This might be not an immediate field of your struct-typed value
but a field of a value which is an immediate field, and so on --
deeper down the hierarchy -- because the json encoder is recursive.

Hence inspect what your value might clinge on which is not *pure data.*
Say, an http.Client instance is not pure data.

Once identified, make sure you convert your existing value you're
encoding to a value of some other type which contains nothing but pure
data values.

[...]
> > I did fmt.println on the data that I am trying to marshall and it
> > looks like this
> >
> > *%!(EXTRA main.JobResponseRoot={{92b4f95b309e8db0f8d56afadefc}
> > http.res {[{{ map[] <nil>}
> > "Date","Time","Time_Zone","Source","Name","Raw_Data"*
> > * <nil> 0x10a3c2d0}]}})*
[...]
> >>> The data I am trying to marshal contains serialized data. This
> >>> data is received in http response.
> >>>
> >>> It works perfectly when I try in version 1.4.1/ 1.4.2.
> >>>
> >>> But when I try this to the latest version 1.6 it gives an error:
> >>> *json: unsupported type: <-chan struct {}*
[...]

User123

unread,
Jun 17, 2016, 3:50:20 AM6/17/16
to golang-nuts
Have found the cause of the issue

The message that I was trying to marshall had 0x10a3c2d0 (when fmt.Println)  which was http response.


Since Go 1.6 http.Request struct contains `Cancel <-chan struct{}` which
results in `json: unsupported type: <-chan struct {}`

So i created struct for httpRes instaed of using interface . 

type HttpRespStruct struct {
Status string
StatusCode int
Proto string
Header http.Header 
ContentLength int64
TransferEncoding []string
}
and it fixed the issue.

One thing i wanted to ask

When i return http response from a function should i use
func makeCalls() (string, error, *http.Response, error) 
or
func makeCalls() (string, error, http.Response, error)  

For the struct i created above what should be the type of header should it be *http.Header  or http.Header ??

Thank You.

Konstantin Khomoutov

unread,
Jun 17, 2016, 2:28:17 PM6/17/16
to User123, golang-nuts
On Fri, 17 Jun 2016 00:50:20 -0700 (PDT)
User123 <twist...@gmail.com> wrote:

[...]
> When i return http response from a function should i use
> *func makeCalls() (string, error, *http.Response, error) *
> or
> *func makeCalls() (string, error, http.Response, error) *
>
> For the struct i created above what should be the type of header
> should it be **http.Header or **http.Header ??*

Most of the code in the net/http package itself pass values of
net/http.Response by pointer, so I'd do the same.

On a side note, can I please ask you to refrain from using that lame
HTML formatting of the Google Groups interface? In a plain mail reader
which quite many people use to read mailing list your message looks
something like [1]. As you could see, guessing which asterisk denotes
a pointer and which is a stupid Google Group's attempt at emphasising
is a bit cumbersome :-)

1. http://www.fastpic.info/image/9b5a0f4dcf
Reply all
Reply to author
Forward
0 new messages