我使用 func Root
中的代码作为创建另一个方法 Login
的指南,如下所示。特别是,在 Root
中,我将文字 Book{}
分配给 b
,然后在 Scan
中使用结果>。该代码不会引发任何错误(尽管我不确定它是否是好的代码),但是当我尝试在 Login
函数中做类似的事情时,我正在修改这个 blogpost ,我得到这个错误
cannot use User literal (type User) as type *User in assignment
为了它的值(value),我在编译时也在上面得到了这个错误
no new variables on left side of :=
但我不是在第二种方法中做同样的事情,即将文字 u := User{}
分配给一个变量,然后在扫描中使用它吗?
您能否解释在赋值中何时可以和不可以使用文字作为类型的代码?
func Root(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT title, author, description FROM books")
books := []Book{}
for rows.Next() {
b := Book{}
err := rows.Scan(&b.Title, &b.Author, &b.Description)
PanicIf(err)
books = append(books, b)
}
...//code ommitted
func Login(password, email string) (u *User, err error) {
u := User{}
db.QueryRow("select * from users where email=$1 ", email).Scan(&u.Id, &u.Password, &u.Email)
if err != nil {
return
}
err = bcrypt.CompareHashAndPassword(u.Password, []byte(password))
if err != nil {
u = nil
}
return
}
最佳答案
简化您的示例以专注于要点:
package main
import "net/http"
type Book struct{}
type User struct{}
func Root(w http.ResponseWriter, r *http.Request) {
books := []Book{}
_ = books
}
func Login(password, email string) (u *User, err error) {
// no new variables on left side of :=
// cannot use User literal (type User) as type *User in assignment
// u := User{}
u = &User{}
return
}
func main() {}
Login
的函数声明声明一个结果参数 u *User
, 指向类型 User
的指针.
u := User{}
statement 是 User
类型的简短变量声明.
The Go Programming Language Specification
A short variable declaration uses the syntax:
ShortVarDecl = IdentifierList ":=" ExpressionList .
It is shorthand for a regular variable declaration with initializer expressions but no types:
"var" IdentifierList = ExpressionList .
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.
由于变量u
已经在同一 block ( u *User
)中声明,编译器会提示 u := User{}
有“no new variables on left side of :=
。”写u = User{}
对于一个简单的任务。
声明books := []Book{}
是新变量的简短变量声明,book
, 在 block 中。
声明u *User
说u
是指向 User
类型变量的指针.
The Go Programming Language Specification
Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated. They consist of the type of the value followed by a brace-bound list of composite elements. An element may be a single expression or a key-value pair.
The LiteralType must be a struct, array, slice, or map type (the grammar enforces this constraint except when the type is given as a TypeName). The types of the expressions must be assignable to the respective field, element, and key types of the LiteralType; there is no additional conversion.
Taking the address of a composite literal generates a pointer to a unique instance of the literal's value.
复合文字 User{}
是 User
类型的文字值, 不是 *User
.编译器提示“cannot use User literal (type User) as type *User in assignment
”。获取指向类型为 User
的文字值的指针的复合文字地址。 (*User
)。写u = &User{}
.
关于go - 在赋值中使用文字作为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25332725/