sibert
sibert

Reputation: 2228

Random reverse output sqlx on Golang

Using sqlx in Golang with this code:

rows, err := db.Queryx(`SELECT "SIGN_ID","SIGN_NAME" FROM sign`)
 for rows.Next() {
    results := make(map[string]interface{})
    err = rows.MapScan(results) 
    fmt.Printf("%#v\n", results)
}

The result looks really promising:

map[string]interface {}{"SIGN_ID":"JD", "SIGN_NAME":"John Doe"}
map[string]interface {}{"SIGN_ID":"JAD", "SIGN_NAME":"Jane Doe"}
map[string]interface {}{"SIGN_ID":"DD", "SIGN_NAME":"Donald Duck"}
map[string]interface {}{"SIGN_NAME":"Chris Walker", "SIGN_ID":"CW"} <----
map[string]interface {}{"SIGN_ID":"SN", "SIGN_NAME":"St Nicolas"}

Two questions:

  1. Why does one line random "reverse" the output order <----:

map[string]interface {}{"SIGN_NAME":"Chris Walker", "SIGN_ID":"CW"}

  1. How can I get rid of the prefix: map[string]interface {}

Upvotes: 0

Views: 104

Answers (1)

Orest Savchak
Orest Savchak

Reputation: 4569

  1. It is known behaviour. Keys are randomly ordered in golang map.
  2. Use fmt.Printf("%v\n", results)

Upvotes: 1

Related Questions