Go: Funcs and Structs

huangapple go评论100阅读模式
英文:

Go: Funcs and Structs

问题

我这里有一段生成的 SQLC 函数和结构体的代码:

const getAccount = `-- name: GetAccount :one
SELECT id, owner, balance, currency, created_at FROM accounts
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetAccount(ctx context.Context, id int64) (Account, error) {
    row := q.queryRow(ctx, q.getAccountStmt, getAccount, id)
    var i Account
    err := row.Scan(
        &i.ID,
        &i.Owner,
        &i.Balance,
        &i.Currency,
        &i.CreatedAt,
    )
    return i, err
}

func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) *sql.Row {
    switch {
    case stmt != nil && q.tx != nil:
        return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...)
    case stmt != nil:
        return stmt.QueryRowContext(ctx, args...)
    default:
        return q.db.QueryRowContext(ctx, query, args...)
    }
}

我正在学习 Golang,并且需要对这段代码中发生的事情进行一些澄清。

根据我的理解,GetAccount 接受两个参数 ctx 和 id,返回一个 Account 模型或错误模型。

在这个函数中,变量 row 被赋值并自动推断为 q.queryRow,并传入了 ctx、q.getAccountStmt、SQL 查询和 id。

那么 q.getAccountStmt 是什么?

而 func (q *Queries) 在这里是什么意思?

另外,为什么将 err 赋值给 row.Scan()?

当代码中写到 return i, err 时,是不是表示会同时返回 i 和 err?

英文:

I have a generated sqlc functions and structs here

const getAccount = `-- name: GetAccount :one
SELECT id, owner, balance, currency, created_at FROM accounts
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetAccount(ctx context.Context, id int64) (Account, error) {
	row := q.queryRow(ctx, q.getAccountStmt, getAccount, id)
	var i Account
	err := row.Scan(
		&i.ID,
		&i.Owner,
		&i.Balance,
		&i.Currency,
		&i.CreatedAt,
	)
	return i, err
}

func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) *sql.Row {
switch {
case stmt != nil && q.tx != nil:
	return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...)
case stmt != nil:
	return stmt.QueryRowContext(ctx, args...)
default:
	return q.db.QueryRowContext(ctx, query, args...)
}
}

I am in process of learning Golang and need some clarification of what is happening in the code.

From my understanding, GetAccount takes 2 parameters ctx and id which returns either a Account Model or error model

In this function, variable row is assigned and automatically inferred to q.queryRow and passes in ctx, q.getAccountStmt, the sql query and id.

So what is q.getAccountStmt?

And What is func (q *Queries) mean here?

Also why is err assigned to row.Scan()?

when it states return i, err; does it mean it will return both i and err?

答案1

得分: 4

这里的func (q *Queries)表示这个函数是一个在指向Queries类型的指针上调用的方法。这个指针被放入变量q中。q类似于JavaScript中的this,但你可以选择变量名。参考Go By Example: MethodsEffective Go: Methods

q.getAccountStmt是获取存储在qgetAccountStmt字段中的值。根据上面的说明,我们知道q是一个指向Queries结构体的指针。参考Go By Example: Structs

为什么将err赋值给row.Scan()?实际上是相反的,row.Scan()的返回值被赋值给errrow.Scan()返回一个错误代码,这是Go通常处理错误的方式。参考Go By Example: ErrorsEffective Go: Errors

row.Scan()之所以返回一个错误,是因为它直接将从数据库行读取的值放入其参数中。这就是为什么它们被传递为指针的原因。参考Go By Example: Pointers

英文:

> What is func (q *Queries) mean here?

It means the function is a method to be called on a pointer to a Queries type. That pointer is put into the variable q. q is like this in Javascript, but you get to choose the variable name. See Go By Example: Methods and Effective Go: Methods.

> What is q.getAccountStmt?

This gets the value stored in the field getAccountStmt from q, which we know from above is a pointer to a Queries struct. See Go By Example: Structs.

> Why is err assigned to row.Scan()?

The other way around, the return value of row.Scan() is assigned to err. row.Scan() returns an error code, this is how Go generally handles errors. See Go By Example: Errors and Effective Go: Errors.

row.Scan() only returns an error because it puts the values read from the database row directly into its arguments. That is why they are passed as pointers. See Go By Example: Pointers.

huangapple
  • 本文由 发表于 2022年11月25日 11:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/74568066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定