So I'm really close to accomplishing my goal. I just need some help converting memory addresses to strings.
Can anyone help me with the last command in this code block? 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) } } On Thursday, September 14, 2017 at 4:59:59 PM UTC-7, Alexandre K wrote: > > Hello Everyone, > > I'm new to Golang and I am trying to figure out how to retrieve multiple > unknown columns and rows from a table. > > I have an example table called ANIMALS > <https://gist.github.com/akalaj/015aa79b5854b728af0baf884f50b827>. > > I am trying to follow the example at the bottom of this GUIDE > <http://go-database-sql.org/varcols.html>. > > Here is the code that I've sewn together: > > 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", > "script:scriptPassword@tcp($HOSTNAME:3306)/Testing") > if err != nil { > log.Println(err) > } > > var str string > rows, err = db.Query("SELECT * FROM animals").Scan(&str) > if err != nil && err != sql.ErrNoRows { > log.Fatal(err) > } > > cols, err := rows.Columns() // Remember to check err afterwards > if err != nil { > log.Fatal(err) > } > > vals := make([]interface{}, len(cols)) > for i, _ := range cols { > vals[i] = new(sql.RawBytes) > } > > for rows.Next() { > err = rows.Scan(vals...) > // Now you can check each element of vals for nil-ness, > if err != nil { > log.Fatal(err) > } > // Here is where I get lost > // How should I access Vals and print the values it finds??? > } > > > > I am trying to get Golang to print the values to the linux terminal using > something like the code below. > > fmt.Println(mysqlValues) > > How do I retrieve the MySQL values stored in the interface in m > <https://gist.github.com/akalaj/28684690c42da8e9aa0cea5bd7340b1c>y code > provided above???? > -- 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.