So, I’ve brushed up on my code at the bottom of the below code block and I am missing something very, very basic (I apologize in advance for my novice’ness).
How do I print memory addresses as strings?
This code is doing exactly what I want it to with the exception of printing the values as strings…How do I get the final fmt.* command to print strings?
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
//Connect to database and check for errors
db, err := sql.Open("mysql","username:password@tcp(127.0.0.1:3306)/Testing")
if err != nil {
log.Println(err)
}
//Execute query, check errs, defer close
rows, err := db.Query("SELECT * FROM animals")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
//Build resultset??
cols, err := rows.Columns() // Remember to check err afterwards
if err != nil {
log.Fatal(err)
}
//Build interface and load it with values
vals := make([]interface{}, len(cols))
for i, _ := range cols {
vals[i] = new(sql.RawBytes)
}
//for every value in 'rows', scan it into the interface
for rows.Next() {
err = rows.Scan(vals...)
// Now you can check each element of vals for nil-ness,
if err != nil {
log.Fatal(err)
}
//The below Println command prints values, but
//it prints them as memory addresses like this => "[0xc42000c600 0xc42000c620]"
//↓↓↓↓How can I print the string values retrieved from MySQL???
fmt.Println(vals) //<<<How do I prevent this guy from printing these^^^^^ memory addresses??
}
}