Errormissing destination name hr_id in *main.Hr (sqlx)

I am trying to use Struct and StructScan (sqlx), but no values are present. Only the struct.

type Hr struct {
	hr_id   int     `db:"hr_id"`
	hr_sign string  `db:"hr_sign"`
	hr_code string  `db:"hr_code"`
	hr_sum  float64 `db:"hr_sum"`
}

func get(query string) interface{} {
	if len(query) > 0 {
		rows, err := db.Queryx("SELECT * FROM hr")
		defer rows.Close()
		fmt.Println(rows)

		hr := Hr{}
		for rows.Next() {
			err := rows.StructScan(&hr)
			if err != nil {
				log("next error" + err.Error())
			}
			fmt.Printf("%#v\n", hr)
		}

		if err != nil {
			log("get error" + err.Error())
		}
		return hr

	}
	return nil
}

The results ar empty:

main.Hr{hr_id:0, hr_sign:β€œβ€, hr_code:β€œβ€, hr_sum:0}
main.Hr{hr_id:0, hr_sign:β€œβ€, hr_code:β€œβ€, hr_sum:0}

And I get this error at log(β€œnext error” + err.Error()):

errormissing destination name hr_id in *main.Hr

What am I doing wrong?

I think you need to capitalize your field names in order for them to be matched.

type Hr struct {
Hr_id int db:"hr_id"
Hr_sign string db:"hr_sign"
Hr_code string db:"hr_code"
Hr_sum float64 db:"hr_sum"
}

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.