El jueves, 6 de septiembre de 2018, 14:59:44 (UTC-5), davidg...@gmail.com 
escribió:
>
> Am looking for insight into why the dataArray_LookupCode output array is 
> partially correct.
> The newUUID() function correctly assigns to each element of the 
> dataArray_LookupCode array, but the *LookupCode field values are all the 
> values of the last element of the LookupCodes data source array. 
> Why?
>
> package main
>
> import (
>     "crypto/rand"
>     "strconv"
>     "fmt"
>     "io"
>     //"encoding/json"
>     //"time"
> )
>
> func newUUID() (string, error) {
>     uuid := make([]byte, 16)
>     n, err := io.ReadFull(rand.Reader, uuid)
>     if n != len(uuid) || err != nil {
>         return "", err
>     }
>     // variant bits; see section 4.1.1
>     uuid[8] = uuid[8]&^0xc0 | 0x80
>     // version 4 (pseudo-random); see section 4.1.3
>     uuid[6] = uuid[6]&^0xf0 | 0x40
>     return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], 
> uuid[8:10], uuid[10:]), nil
> }
>
> type LookupCode struct
> {
>   CodeId    string    `json:"codeid"`    
>   Description    string    `json:"description"`
>   Active    bool    `json:"active"`
> }
>
> type UUID_LookupCode struct
> {
>     UUID string `json:"uuid"`
>     lookup    *LookupCode `json:"lookup"`
>     // *LookupCode  [does not work]
> }
>
> var LookupCodes []LookupCode
> var dataArray_LookupCode []UUID_LookupCode
>
> func init() { 
>     LookupCodes = []LookupCode {
>         LookupCode {
>         Active: true,
>         CodeId: "USA_00",
>         Description: "All US States",
>         },
>         LookupCode {
>         Active: true,
>         CodeId: "USA_01",
>         Description: "All US Western compact States",
>         },
>     }
>
>     dataArray_LookupCode := make([]UUID_LookupCode, 0)
>     for _, xt1 := range LookupCodes {
>         c1 := &UUID_LookupCode{}
>         c1.lookup = &xt1
>         //c1.LookupCode.CodeId = xt1.CodeId
>         typeKey, err := newUUID()
>         if err == nil {
>             c1.UUID = typeKey
>         }
> fmt.Println(c1.lookup.CodeId )  // correct output: USA_00 USA_01
>         dataArray_LookupCode = append(dataArray_LookupCode, *c1) 
>     }
> fmt.Printf(strconv.Itoa(len(dataArray_LookupCode)))  // correct output: 2
> fmt.Println(dataArray_LookupCode[0].lookup.CodeId,dataArray_LookupCode[1].lookup.CodeId)
>   
> // incorrect output: USA_01 USA_01
>
> }// init
>
> func main() {
> }
>
>

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

Reply via email to