title: Golang连接MySQL查询提示: missing destination name age in *[]main.Person
date: 2023-01-07 01:18:53.0
updated: 2023-01-07 01:20:28.0
url: https://liumou.site/doc/597
categories:

  • Mariadb
  • GO
    tags:
  • Go
  • Mariadb

数据库数据

image-1673025449657

有问题的结构体

type Person struct {
    UserId   int    `db:"id"`
    Username string `db:"name"`
    age      int    `db:"age"`
}

image

正常的结构体

type Person struct {
    UserId   int    `db:"id"`
    Username string `db:"name"`
    UserAge  int    `db:"age"`
}

image-1673025410368

原因

  • ,结构体设置的变量名称与数据库的字段名称不能相同
  • 当然网上还有一种说法是字段名称没有加双引号也会提示此类错误