Handle mismatched type of type [] String, bson and time.time in golang

0

Good afternoon colleagues, I come with the following question I am developing in an Iris framework and I have encountered the following problem, the manipulation of data types  my code is this:

//ExpresionRegularMgo estructura de ExpresionRegulars mongo
type ExpresionRegularMgo struct {
    ID        bson.ObjectId 'bson:"_id,omitempty"'
    Id        string        'bson:"ID"'
    Nombre    string        'bson:"Nombre"'
    Expresion string        'bson:"Expresion"'
    Etiquetas []string      'bson:"Etiquetas"'
    Estatus   bson.ObjectId 'bson:"Estatus"'
    FechaHora time.Time     'bson:"FechaHora"'
}

//ExpresionRegularElastic estructura de ExpresionRegulars para insertar en Elastic
type ExpresionRegularElastic struct {
    Id        string        'json:"ID"'
    Nombre    string        'json:"Nombre"'
    Expresion string        'json:"Expresion"'
    Etiquetas []string      'json:"Etiquetas"'
    Estatus   bson.ObjectId 'json:"Estatus"'
    FechaHora time.Time     'json:"FechaHora"'
}

At the moment of concatenating the strings I have problems in the types [] string, bson.ObjectId and time.Time, I am concatenated in such a way that they are displayed on my page in the following way

func GeneraTemplatesBusqueda(ExpresionRegulars []ExpresionRegularMgo) (string, string) {
    floats := accounting.Accounting{Symbol: "", Precision: 2}
    cuerpo := ''

    cabecera := '<tr>
            <th>#</th>

                <th>Id</th>                 

                <th>Nombre</th>                 

                <th>Expresion</th>                  

                <th>Etiquetas</th>                  

                <th>Estatus</th>                    

                <th>FechaHora</th>                  
                </tr>'

    for k, v := range ExpresionRegulars {
        cuerpo += '<tr id = "' + v.ID.Hex() + '" onclick="window.location.href = '/ExpresionRegulars/detalle/' + v.ID.Hex() + '';">'
        cuerpo += '<td>' + strconv.Itoa(k+1) + '</td>'
        cuerpo += '<td>' + v.Id + '</td>'

        cuerpo += '<td>' + v.Nombre + '</td>'

        cuerpo += '<td>' + v.Expresion + '</td>'

        cuerpo += '<td>' + v.Etiquetas + '</td>'

        cuerpo += '<td>' + v.Estatus + '</td>'

        cuerpo += '<td>' + v.FechaHora + '</td>'

        cuerpo += '</tr>'
    }

    return cabecera, cuerpo
}

However I have problems regarding the handling of types in v. Labels, V. Statutes and v. Date Time, what could be the way to concatenate those objects in the output to show the html?

Thanks in advance and regards

    
asked by Fatima Reyes 26.09.2017 в 19:07
source

1 answer

0

To do this correctly, it would be better to use the fmt (formatting package) that allows exactly what you are trying. The function you must use is the fmt.Sprintf, where the first parameter is the string you want to enter with% v where the parameters of the Mongo database go and then the parameters are separated by a comma

func GeneraTemplatesBusqueda(ExpresionRegulars []ExpresionRegularMgo) (string, string) {
//floats := accounting.Accounting{Symbol: "", Precision: 2}
cuerpo := ''

cabecera := '<tr>
        <th>#</th>

            <th>Id</th>                 

            <th>Nombre</th>                 

            <th>Expresion</th>                  

            <th>Etiquetas</th>                  

            <th>Estatus</th>                    

            <th>FechaHora</th>                  
            </tr>'

for k, v := range ExpresionRegulars {
    cuerpo += fmt.Sprintf('<tr id = "%v" onclick="window.location.href = '/ExpresionRegulars/detalle/%v';">', v.ID.Hex(), v.ID.Hex())
    cuerpo += fmt.Sprintf('<td> %v </td>', k+1)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.Id)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.Nombre)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.Expression)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.Etiquetas)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.Estatus)
    cuerpo += fmt.Sprintf('<td> %v </td>', v.FechaHora)
    cuerpo += '</tr>'
}
    
answered by 17.01.2018 в 15:01