Get object string value from interface{} [SOLVED]

Hey there, I’m trying to get an object value, which is a *string stored in an interface{}, pass it to a function, handle this string inside the function and change the object value with the function return. Below is my code;

package main

import (
	"encoding/json"
	//	"fmt"
	"net/http"
	"strings"
)

func teste(param interface{}) interface{} {

	return param.(*string) + "TESTE"
}

func ultimaposicao(w http.ResponseWriter, r *http.Request) {

	urlqry := r.URL.Query()

	JsonUltimaPosicao := ObjUltimaPosicao{}

	//faz a validação da chave do rest
	if !RestVerificaChave(urlqry.Get("chave")) {
		JsonUltimaPosicao.Status.Cod = "-2"
		JsonUltimaPosicao.Status.Msg = "Falha durante tentativa de autenticacao no REST! Credenciais invalidas."
		json.NewEncoder(w).Encode(&JsonUltimaPosicao)
		return
	}
	//faz a validação da chave do rest

	Satelital := "0" //PADRAO GPRS, ADAPTACAO PARA VERSOES ANTIGAS DO APLICATIVO
	if strings.TrimSpace(urlqry.Get("satelital")) != "" {
		Satelital = urlqry.Get("satelital")
	}

	rows, err := db.Query("EXEC PRCATUALIZACOES @IDVEICULO=" + urlqry.Get("idveiculo") + ", @TAMANHOPAGINA=1, @NUMEROPAGINA=1, @SATELITAL=" + Satelital)

	defer rows.Close()

	if err != nil {
		GeraLog(err.Error())
		return
	}

	if rows.Next() {
		columns, err := rows.ColumnTypes()
		if err != nil {
			GeraLog(err.Error())
			return
		}

		values := make([]interface{}, len(columns))
		object := map[string]interface{}{}

		for i, column := range columns {
			object[column.Name()] = new(*string)
			values[i] = object[column.Name()]
		}

		err = rows.Scan(values...)
		if err != nil {
			GeraLog(err.Error())
			return
		}

		for i, column := range columns {
			if column.Name() == "TENSAO" {
				values[i] = teste(values[i])
			}
			//fmt.Println(column.Name() + " : " + object[column.Name()].(string))
		}

		JsonUltimaPosicao.Status.Cod = "1"
		JsonUltimaPosicao.Status.Msg = "Sucesso."
		JsonUltimaPosicao.Info = object
	} else {
		JsonUltimaPosicao.Status.Cod = "-3"
		JsonUltimaPosicao.Status.Msg = "Nenhuma posicao ainda registrada para este alvo, ou o alvo informado e invalido."
	}

	json.NewEncoder(w).Encode(&JsonUltimaPosicao)
}

At the 66th line, if column name is TENSAO, I need to get this string, pass to function teste(), and change values[i] value with the functon result. Could anyone help me to understand how to do it?

Tnx.

You’re code is utterly difficult and confusing to read.

You have not initialised your objects map, and have two for loops both iterating over the same columns slice, why?

You set an empty *string to your object map, pass it to values slice, which you then use as your dest for rows.Scan(), and then pass it to your struct lower down, whats its purpose?

What is the structure of your database, and what value do your need to change?

1 Like

Why my code is “utterly difficult and confusing” to read? What is not an “utterly confusing” code for you? I told on my post which line in the code theproblem is, the code is working, I just need to get the scanned value, pass this value to a function, handle this value and change the object value with the function return… The database structure you don’t need to know that information to help me, the field is a VARCHAR field, field called TENSAO,if you read the code where I told you’ll understand what I need.

I need help only where I asked for help, at the 66th line;

		for i, column := range columns {
			if column.Name() == "TENSAO" {
				values[i] = teste(values[i])
			}
			//fmt.Println(column.Name() + " : " + object[column.Name()].(string))
		}

You problem is not on line 66, because this

doesn’t compile. You can’t append a string to a string pointer.

Your code is confusing because there is a lot of it, none of it has line numbers the way you’re presenting it, some of it is commented out, it’s partly in Portuguese and commented as such, and it’s not gofmt-formatted. You’ll do a lot better in getting answers if you narrow down your problem to a reproducible example on the Go playground.

As to your actual problem, when you’re fairly new to Go it’s almost definitely the wrong answer to try to do everything as interface{}, type assertions, and string pointers. This is unnecessarily tricky and gets you into trouble.

If you insist, here is something to get you started: Go Playground - The Go Programming Language

The fact you never declared beforehand that your working with a VARCHAR field, is why I asked for the db structure, for all we know you could be using this function to parse an int into a string… Please take it as constructive criticism as after all your the one asking for help

You need to dereference your interface

*param.(*string) + "TESTE"

https://play.golang.org/p/ppi7q1wV9p

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.