Re: [go-nuts] How to create unique IDs when ranging over data in Golang and use in Javascript

2017-03-08 Thread Rejoy Nair
Yes..thats the link.. :)  was a bit short on time.. so posted the question
in multiple forums.. was struggling a bit manipulating html elements .. so
was kind of overwhelmed by the answer provided...

On Tue, Mar 7, 2017 at 4:00 PM, Konstantin Khomoutov <
flatw...@users.sourceforge.net> wrote:

> On Tue, 7 Mar 2017 15:42:11 +0530
> Rejoy Nair <rejo...@gmail.com> wrote:
>
> > Thanks a lot!. Your answers have been helpful. I was also provided
> > with a very good solution from another forum that I 'll post that
> > here for the benefit for anybody who 'd come across this post.
>
> You mean <http://stackoverflow.com/a/42642988/720999>, don't you? ;-)
>
> > Its on the lines of what you 'd suggested. Till  then I was actually
> > beginning to think that this wasn't even really a Golang question.
>
> It can be deemed as being related to the stock Go templating, so that's
> OK even though the essense of your problem wasn't Go-specific.
>
> [...]
>

-- 
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] How to create unique IDs when ranging over data in Golang and use in Javascript

2017-03-07 Thread Rejoy Nair
Thanks a lot!. Your answers have been helpful. I was also provided with a
very good solution from another forum that I 'll post that here for the
benefit for anybody who 'd come across this post. Its on the lines of what
you 'd suggested. Till  then I was actually beginning to think that this
wasn't even really a Golang question.

for creating unique id for each row

{{range $index, $value := .}}...Update...{{end}

sending the current form or row id as a parameter to know which form
was submitted when the save_data() event fires.

{{range $index, $value := .}}...{{end}}


function save_data(form, rowno) {
  var input = document.getElementById("pid"+rowno);
  localStorage.setItem("id", input.value); }



On Tue, Mar 7, 2017 at 1:23 PM, Konstantin Khomoutov <
flatw...@users.sourceforge.net> wrote:

> On Mon, 6 Mar 2017 23:30:20 -0800 (PST)
> Rejoy  wrote:
>
> > I am getting data from the server and iterating over it to create a
> > table and then I am using a form to store an id to local storage
> > using javascript. Here is code snippet
> >
> > 
> > Product ID
>
> This loop
>
> > {{range .}}
> >  > class="btn btn-info pid" id="pid" name="{{.puid}}"
> > value="{{.puid}}">Update {{end}}
>
> would create N table rows with N buttons all having the same "id"
> attribute set to "pid", so this callback
>
> [...]
> > function save_data() {
> >   var input = document.getElementByID("pid");
> >   localStorage.setItem("id", input.value);
> > }
> [...]
>
> will always find the topologically first element by that attribute.
> Isn't that your exact problem then?
>
> > However every time, no matter of which table row's "update" button I
> > click, everytime only the ID of the first table row element is
> > getting stored. Is there a way I can generate unique IDs and
> > reference it in Javascript when ranging over the data. Thanks
>
> I'd say, in your loop, you should roll like this:
>
> 1) Increase some simple counter variable initially set to, say 0.
>
> 2) Use it to generate the values for the "id" attributes of your
>buttons.
>
> 3) Set callback in a way to pass it the exact attribute value to search
>for, like with
>
>  form onsubmit="save_data($pid)
>
> 4) In the callback, use the supplied value directly.
>
> Or maybe just not reinvent the wheel and use some JS library which
> would produce data bindings for you do you don't have to wire explicit
> callbacks etc.
>

-- 
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] Parse JSON in Template

2017-02-19 Thread Rejoy Nair
Yes, there could be ways to do it. I could have used Javascript to access
the JSON data values. But couldn't get that to work either. So the easier
thing for me to do was to unmarshal the string again into a
map([string]interface{}) type which is what I finally did to get the code
to work.
Thanks for your reply though.
Rejoy

On Sun, Feb 19, 2017 at 10:04 PM, Hugh S. Myers  wrote:

> The devil sitting on my shoulder tempts me to say "Of course, there is"
> and walk away. That said, by now there must exist code to read JSON (YAML,
> Windows config files, etc.) from a string. It's just a matter of finding
> it.
>
>- https://gobyexample.com/json
>- https://blog.golang.org/json-and-go
>
> are just the first two examples that are returned by a Google search which
> mentions 'About 2,890,000 results (0.54 seconds) '. The query was 'go
> code to read JSON'
> HTH and good luck!
>
> --hsm.
>
> On Sun, Feb 19, 2017 at 9:17 AM, Rejoy  wrote:
>
>> I am retrieving a set of field values from the Database into an interface
>> type object. One of the field values is a JSON string (say Order detail). I
>> am retrieving it as a string as passing it on to a template and referencing
>> it using {{.orderdetail}}
>> this is the output I get on the client which is a valid json:
>> [{"pid":"d6742e4e-2ad6-43c5-97f4-e8a7b00684e2","image":"1App
>> leiphone7.jpeg","name":"iphone7","price":7,"count":1},{"
>> pid":"12d3d8fc-66b6-45f9-a91b-d400b91c32aa","image":"
>> 2SamsungGalaxys7.jpeg","name":"SamsungGalaxy7","price":7,"count":1}]
>> I am not able to access the values in the json using the keys at client
>> side in the template. {{.orderdetails.pid}} does't yield and value.
>> One way would be to unmarshal the string into a map([string]interface{})
>> and then pass on the data to the templates. But was curious if there is a
>> way to access the data using the JSON keys.
>> Thanks
>> Rejoy
>>
>> --
>> 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.
>>
>
>

-- 
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] Blabk values when trying to pass a slice to a struct field

2017-02-03 Thread Rejoy Nair
Your post gives a lot of clarity on key concepts. The example where you
make use of the nil value of an uninitialized slice within a struct is a
really good one. Thanks so much!

On Fri, Feb 3, 2017 at 6:30 PM, Konstantin Khomoutov <
flatw...@users.sourceforge.net> wrote:

> On Fri, 3 Feb 2017 17:48:18 +0530
> Rejoy Nair <rejo...@gmail.com> wrote:
>
> > > > I am trying to pass a slice of data to a struct field . The field
> > > > has a type of another struct. I get blank values in the output.
> > > >
> > > > Here is the code: https://play.golang.org/p/-YZ7UUI--D
> > > >
> > > > I 'd like to understand how to pass on the slice data to a struct
> > > > field.
> > >
> > > It's not clear what do you want to achieve as the question mentions
> > > slices, the code example deals with JSON unmarshaling and unrelated
> > > value juggling.
> > >
> > > But is <https://play.golang.org/p/FxcA0vW9ZU> what you're after?
> > > (See line 36 there.)
> >
> > Thanks!. thats what I was looking for.
> >
> >  I had tried using the assignment Dt.P = Pr before the unmarshalling
> > of the JSON to the variable pdata and it would not work.
>
> I see.
>
> Then, you absolutely positively need to read [1] and then [2] --
> in this order.
>
> As to your particular case, here's a bunch of related facts to ponder:
>
> * json.Unmarshal() is told to unmarshal into a slice value.
>
> * The slice it's passed is of length 0, which means that function has
>   to perform several appends to that slice (two, in fact).
>
> * Appending to a slice in Go _may update the slice value_ because it
>   might result in relocation of the slice's backing array.
>
> * In your case it works because json.Unmarshal() was passed not a slice
>   value itself but a pointer to it (or, to put it differently, an
>   address of the slice value kept in memory somewhere).
>
>   Because of this, when json.Unmarshal() appends new elements to your
>   slice it merely re-writes that slice's value each time -- via the
>   pointer supplied to it.
>
> * When json.Unmarshal() exits, the slice value whose address was passed
>   to that function now represents the "final" state of the slice --
>   filled with values.
>
>   We then take it and assign it to a field of your custom struct value.
>   Now the original slice value and that field represent the same
>   slice (and refer to the same underlying array).
>
> * If you do this "backwards" -- like you supposedly did by doing
>   something like
>
> Pr = make([]Prdata, 0)
> Dt.P = Pr
> json.Unmarshal(..., )
>
>   you ended up in a situation where json.Unmarshal() changed the value
>   at Pr one or more times when it was appending new elements to that
>   slice, and the slice value ended up being kept in Pr was different
>   from the value kept in the field Dt.P (which supposedly was still
>   nil).
>
> * With all these things considered, it's better to rewrite your code
>   to produce [4], or simplify it even further by relying on the fact
>   appending to an uninitialized slice value (nil) allocates the
>   underlying array for that slice as if you called make() for it;
>   this way we can simply initialize only what matters in your custom
>   struct value and let json.Unmarshal() handle slice allocation [5].
>
> TL;DR
>
> Doing
>
>   a := make([]whatever)
>   b := append(a, elem)
>
> is allowed to produce b != a, and that's why appending to a slice
> most of the time is done using
>
>   a = append(a, elem)
>
> In your case this effect was obscured by appending done in a function
> you did not write but that was due to the fact you weren't familiar
> with how slices work in Go, so please work through [1, 2].
>
> 1. https://blog.golang.org/slices
> 2. https://blog.golang.org/go-slices-usage-and-internals
> 3. https://play.golang.org/p/kaTjPTE2Hx
> 4. https://play.golang.org/p/Z6B8JUS9ON
>

-- 
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] Blabk values when trying to pass a slice to a struct field

2017-02-03 Thread Rejoy Nair
Thanks!. thats what I was looking for.

 I had tried using the assignment Dt.P = Pr before the unmarshalling of the
JSON to the variable pdata and it would not work.

On Fri, Feb 3, 2017 at 5:18 PM, Konstantin Khomoutov <
flatw...@users.sourceforge.net> wrote:

> On Fri, 3 Feb 2017 03:32:00 -0800 (PST)
> Rejoy  wrote:
>
> > I am trying to pass a slice of data to a struct field . The field has
> > a type of another struct. I get blank values in the output.
> >
> > Here is the code: https://play.golang.org/p/-YZ7UUI--D
> >
> > I 'd like to understand how to pass on the slice data to a struct
> > field.
>
> It's not clear what do you want to achieve as the question mentions
> slices, the code example deals with JSON unmarshaling and unrelated
> value juggling.
>
> But is  what you're after?
> (See line 36 there.)
>

-- 
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.