golang查询数据库没有数据时返回值问题

在使用Golang的database/sql包操作MySQL数据库时,如果查询无结果,会返回err为nil,表示没有错误。rows变量并不会返回nil,而是一个结构体的内存地址,表明查询操作已完成但未找到记录。

使用golang的datadase/sql包进行mysql数据库查询,没有查询到结果时,返回值是多少

sqlStr := "select distinct domain from securitymanager.domainthreat where ip =?"
	rows, err := db.Query(sqlStr, "222.73.219.63")

没有查询到数据时返回值:

  1. 返回的err为nil,没有查询到数据是正常的情况,不会报错
  2. rows不是nil!而是一个结构体的内存地址,比如&{0xc042096080 0x4c89e0 0xc042078140 0x4ba8d0 0xc04203a1c0 {{0 0} 0 0 0 0} false <nil> []}
  3. rows结构体格式
type Rows struct {
	dc          *driverConn // owned; must call releaseConn when closed to release
	releaseConn func(error)
	rowsi       driver.Rows
	cancel      func()      // called when Rows is closed, may be nil.
	closeStmt   *driverStmt // if non-nil, statement to Close on close

	// closemu prevents Rows from closing while there
	// is an active streaming result. It is held for read during non-close operations
	// and exclusively during close.
	//
	// closemu guards lasterr and closed.
	closemu sync.RWMutex
	closed  bool
	lasterr error // non-nil only if closed is true

	// lastcols is only used in Scan, Next, and NextResultSet which are expected
	// not to be called concurrently.
	lastcols []driver.Value
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值