2

I am trying to implement it in Go.how can i transpose the result but i have a problem in my code.how can i help me.

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

this is function for tranpose the query when it fetch data from database

func transpose(slice [][]byte) [][]byte {
    xl := len(slice[0])
    yl := len(slice)
    result := make([][]byte, xl)
    for i := range result {
        result[i] = make([]byte, yl)
    }
    for i := 0; i < xl; i++ {
        for j := 0; j < yl; j++ {
            result[i][j] = slice[j][i]
        }
    }
    return result
 }

I connect to the database

func main() {
    db, err := sql.Open("mysql", "root:MyNewPassword1234567?@tcp(127.0.0.1:3306)/all_re")
    defer db.Close()

    if err != nil {
        fmt.Println("Failed to connect", err)
        return
    }

    rows, err := db.Query(`SELECT * from tdb`)
    if err != nil {
        fmt.Println("Failed to run query", err)
        return
    }

    cols, err := rows.Columns()
    if err != nil {
        fmt.Println("Failed to get columns", err)
        return
    }
    //fmt.Println(cols) 

Result is your slice string.the result show as all of them show by row.

 rawResult := make([][]byte, len(cols))
 result := make([]string, len(cols))

A temporary interface{} slice.

dest := make([]interface{}, len(cols)) 
        for i, _ := range rawResult {
            dest[i] = &rawResult[i] // Put pointers to each string in the interface slice
        }
        for rows.Next() {
            err = rows.Scan(dest...)
            if err != nil {
                fmt.Println("Failed to scan row", err)
                return
            }
            //fmt.Println(dest...)
    
            for i, raw := range rawResult {
                if raw == nil {
                    result[i] = "\\N"
                } else {
                    result[i] = string(raw)
                }
                fmt.Println(result[i]) 
                 ar := transpose(rawResult)   
                fmt.Println(ar)
            }
        }
    
    }

Required output:

       date       | 2022-01-07  |  2022-01-08    
    -----------------------------------------------------------------------
       DayOfWeek  | Friday      |  Saturday   
       col1       | 1           |  20 
       col2       | 41          |  38 
       col3       | NULL        |  1458
Larsiya
  • 79
  • 5

1 Answers1

0

Since you want to Transpose slice of byte from (n x m) To (m x n). There are various approach that you can apply as per your requirements.

Your Transpose function is perfectly right. I tested it by passing [][]string but In your case it is expecting data in byte form. Working with byte is bit difficult compare to string :

   date        | DayOfWeek  |  col1   | col2    |col3
-----------------------------------------------------------------------
   2022-01-07  | Friday     |  1      |  41     |  NULL
   2022-01-08  | Saturday   |  20     |  38     |  1458     

You can check transpose function here : https://go.dev/play/p/FZb69xuEnf_L

Fetch your data from DB into []map[string]interface{}. And do whatever operation you want to perform with that data.

I have created a sample example for your scenario as follows :

func main() {

    example := []map[string]interface{}{
        map[string]interface{}{
            "Name":    "James",
            "SurName": "Camara",
            "Done":    "yes",
        },
        map[string]interface{}{
            "Name":    "ashutosh",
            "SurName": "role",
            "Done":    "no",
        },
    }

    sm := []string{"Name", "SurName", "Done"}
    mp := make([]interface{}, 3)
    for i := 0; i < 3; i++ {
        maza := make([]interface{}, 3)
        maza[0] = sm[i]
        for j := 0; j < 2; j++ {
            maza[j+1] = example[j][sm[i]]
        }
        mp[i] = maza
    }
    fmt.Println(mp)

}

Output :

//your required output is matching here
[[Name James ashutosh] [SurName Camara role] [Done yes no]]

You can run it here : https://go.dev/play/p/rN3RyyIWu95

Ashutosh Singh
  • 721
  • 3
  • 6