[go-nuts] Arrays
Dear Gophers, I am a beginner gopher has created a project of Connect Five. The game's board is shown in a dot array like this ... ... ... And to input, you have to put in the exact coordinates of the place you want to place, like this 1 3. You would have to carefully count the dots and then input, which is a pain for the eyes. So I wonder if it is possible to make a array like this and how: 11 12 13 14 21 22 23 24 31 32 33 34 Thank you, John -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Arrays, structs and marshalJSON
aah. Damn it :-) Thanks a lot. I am hitting myself for missing this. 2017-08-09 16:40 GMT+05:30 Jakob Borg: > On 9 Aug 2017, at 12:26, Sankar wrote: > > > > Now for the same, Item object, the json.Marshal function returns two > different strings for the `Item` object. > > > > If you run the playground url, you will find that: > > > > {"Items":[{"SD":"2009-11-10T23:00"}]} > > {"Item":{"SD":"2009-11-10T23:00:00Z"}} > > > > are two different outputs (notice the "seconds" and the "Z" in the > second line). I expected both the lines to print the same output for "SD" > as the same object is re-used for them both. > > > > Now, should I define MarshalJson and UnmarshalJSON function for the > []Item too (after making it a custom struct) ? > > > > Or is this a bug, or do I misunderstand anything ? > > The issue is that you have defined MarshalJSON as a method with a pointer > receiver, while the JSON marshaller will access it through an interface > value that is not addressable. Instead, declare the MarshalJSON method with > a value receiver: > > func (ct CustomTime) MarshalJSON() ([]byte, error) { ... } > > //jb > > -- Sankar P http://psankar.blogspot.com -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Arrays, structs and marshalJSON
On 9 Aug 2017, at 12:26, Sankarwrote: > > Now for the same, Item object, the json.Marshal function returns two > different strings for the `Item` object. > > If you run the playground url, you will find that: > > {"Items":[{"SD":"2009-11-10T23:00"}]} > {"Item":{"SD":"2009-11-10T23:00:00Z"}} > > are two different outputs (notice the "seconds" and the "Z" in the second > line). I expected both the lines to print the same output for "SD" as the > same object is re-used for them both. > > Now, should I define MarshalJson and UnmarshalJSON function for the []Item > too (after making it a custom struct) ? > > Or is this a bug, or do I misunderstand anything ? The issue is that you have defined MarshalJSON as a method with a pointer receiver, while the JSON marshaller will access it through an interface value that is not addressable. Instead, declare the MarshalJSON method with a value receiver: func (ct CustomTime) MarshalJSON() ([]byte, error) { ... } //jb -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Arrays, structs and marshalJSON
Hi, There is a difference in the way MarshalJSON of a struct is called when an instance of a struct is either part of an array or embedded directly inside another object. I am not sure if it is a bug or if it is the intended behavior. Please see the Go source available at: https://play.golang.org/p/NpWj4VAjrz I have a custom struct `CustomTime` which has a MarshalJSON and an UnmarshalJSON function. Now I have a struct `Item`, which has an instance of the above `CustomTime` struct. There are two other structs, ItemsArray and ItemsMember where Item is part of an array or a direct member. type ItemsArray struct { Items []Item } type ItemsMember struct { Item Item } Now for the same, Item object, the json.Marshal function returns two different strings for the `Item` object. If you run the playground url, you will find that: {"Items":[{"SD":"2009-11-10T23:00"}]} {"Item":{"SD":"2009-11-10T23:00:00Z"}} are two different outputs (notice the "seconds" and the "Z" in the second line). I expected both the lines to print the same output for "SD" as the same object is re-used for them both. Now, should I define MarshalJson and UnmarshalJSON function for the []Item too (after making it a custom struct) ? Or is this a bug, or do I misunderstand anything ? Thanks. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.