Saturday, March 25, 2023
HomeGolangRuntime producing json information without framework - Obtaining Assistance

Runtime producing json information without framework – Obtaining Assistance


‘func queryToJson( db * sql.DB, question string, args … user interface {}) ([] byte, mistake) {
// a range of JSON items
// the map trick is the area name
var items [] map[string] user interface {}

 rows, err:= db.Query( question, args ...).
if err!= nil {
return nil, err.
}
for rows.Next() {
// identify what columns were returned.
// the column names will certainly be the JSON things area tricks.
columns, err:= rows.ColumnTypes().
if err!= nil {
return nil, err.
}

// Check demands a range of guidelines to the worths it is establishing.
// This produces the things as well as establishes the worths properly.
worths:= make([] user interface {}, len( columns)).
things:= map[string] user interface {} {}
for i, column:= variety columns {
things[column.Name()] = reflect.New( column.ScanType()). User Interface().
worths[i] = things[column.Name()]
}

err = rows.Scan( worths ...).
if err!= nil {
return nil, err.
}

items = append( items, things).
}

// indent since I wish to check out the outcome.
return json.MarshalIndent( items, "", "t").

}

This code is functioning. I desire column ought to be as in data source table areas order.


This subject was instantly shut 90 days after the last reply. New replies are no more permitted.

RELATED ARTICLES

Most Popular

Recent Comments