How to initialize a composite structure with json tags

0

I want to initialize a composite structure. The composite structure has json tags, and when running the program it shows something like the following:

go run struct.go 
# command-line-arguments
./struct.go:23:11: cannot use struct { Lat float32; Lon float32 } literal (type struct { Lat float32; Lon float32 }) as type struct { Lat float32 "json:\"lat\""; Lon float32 "json:\"lon\"" } in field value

Someone who can tell me what I'm missing or what I'm doing wrong?

The program with which I am doing tests is the following:

package main

import "fmt"

type Document struct {
    Ciudad     string 'json:"ciudad"'
    Colonia    string 'json:"colonia"'
    Cp         int 'json:"cp"'
    Delegacion string 'json:"delegacion"'
    Location   struct {
        Lat float32 'json:"lat"'
        Lon float32 'json:"lon"'
    } 'json:"location"'
}

func main() {

    d := &Document{
        Ciudad: "xxxx xxxx",
        Colonia: "yyyy yyyy",
        Cp: 3333,
        Delegacion: "zzzz zzzz",
        Location: struct {
            Lat float32
            Lon float32
        }{
            Lat: -111.111, 
            Lon: 111.111,
        },

    }

    fmt.Printf("%v", d)

}

Here I leave the league with the program in the playground version of go:

link

    
asked by Sanx 28.08.2017 в 12:57
source

1 answer

1

Good morning, define your Location object outside of the struct ' Document and to assign obvious values to the part of the code in which you return to define the Location object.

You would have something like this:

package main

import "fmt"

type Document struct {
    Ciudad     string 'json:"ciudad"'
    Colonia    string 'json:"colonia"'
    Cp         int 'json:"cp"'
    Delegacion string 'json:"delegacion"'
    Location   'json:"location"'
}

type Location struct{
    Lat float32 'json:"lat"'
    Lon float32 'json:"lon"'
}



func main() {

    d := &Document{
        Ciudad:     "xxxx xxxx",
        Colonia:    "yyyy yyyy",
        Cp:         3333,
        Delegacion: "zzzz zzzz",
        Location: Location {
            Lat: -111.111,
            Lon: 111.111,
        },
    }

    fmt.Printf("%v", d)

}

And it would give the following output:

  

& {xxxx xxxx yyyy yyyy 3333 zzzz zzzz {-111.111 111.111}}

Here is the demo link

I hope I have helped you.

EDIT: trying a little more I've got the next exit (which I think is the one you wanted in the first place).

  

{"city": "xxxx xxxx", "colony": "yyyy yyyy", "cp": 3333, "delegation": "zzzz zzzz", "location": {"lat": - 111.111, " lon ": 111,111}}

To do this you must import encoding / json and use the json.Marshal () method

The code is as follows:

package main

import "fmt"
import "encoding/json"

type Document struct {
    Ciudad     string 'json:"ciudad"'
    Colonia    string 'json:"colonia"'
    Cp         int 'json:"cp"'
    Delegacion string 'json:"delegacion"'
    Location   'json:"location"'
}

type Location struct{
    Lat float32 'json:"lat"'
        Lon float32 'json:"lon"'
}



func main() {

    d := &Document{
        Ciudad:     "xxxx xxxx",
        Colonia:    "yyyy yyyy",
        Cp:         3333,
        Delegacion: "zzzz zzzz",
        Location: Location {
            Lat: -111.111,
            Lon: 111.111,
        },
    }
    jso,_ := json.Marshal(d)
    fmt.Printf("%v", string(jso))

}

And here the demo link

    
answered by 28.08.2017 в 13:24