gpt4 book ai didi

go - 附加到结果 slice

转载 作者:IT王子 更新时间:2023-10-29 02:00:20 24 4
gpt4 key购买 nike

我正在尝试创建一个可以传递给模板以显示给用户的 sql 结果片段。我有以下内容:

type Post struct {
Title string
}

func landing(w http.ResponseWriter, r *http.Request){
posts := make([]Post, 0)
conn := OpenConnection()
defer conn.Close()
rows, err := conn.Query("SELECT p.title FROM posts p LIMIT 100")
if err != nil {
fmt.Println(err)
} else {
for rows.Next() {
var title string
rows.Scan(&title)
posts := append(posts, Post{Title: title}) //error thrown here
}
}
t, _ := template.ParseFiles("home.html")
t.Execute(w, posts)
}

func main() {
http.HandleFunc("/", landing)
}

编译时出现错误posts declared and not used。如果我在 append 调用之后 fmt.Println(posts) 编译,但它似乎是在每次迭代时重置 posts 的值而不是追加。

正确的做法是什么?

最佳答案

Declarations and scope

A declaration binds a non-blank identifier to a constant, type, variable, function, or package. Every identifier in a program must be declared. No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block.

Declaration   = ConstDecl | TypeDecl | VarDecl .
TopLevelDecl = Declaration | FunctionDecl | MethodDecl .

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

Go is lexically scoped using blocks:

  • The scope of a predeclared identifier is the universe block.
  • The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block.
  • The scope of an imported package identifier is the file block of the file containing the import declaration.
  • The scope of an identifier denoting a function parameter or result variable is the function body.
  • The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.
  • The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block.

An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration.

Short variable declarations

A short variable declaration uses the syntax:

ShortVarDecl = IdentifierList ":=" ExpressionList .

It is a 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 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.

您在内部范围内有一个简短的 posts 变量声明。因此,内部作用域中posts的短变量声明没有被使用。

posts := make([]Post, 0)
{
posts := append(posts, Post{Title: title}) //error thrown here
}

您想要对 posts 进行赋值,即在外部作用域中声明的变量,在内部作用域中。

posts := make([]Post, 0)
{
posts = append(posts, Post{Title: title})
}

关于go - 附加到结果 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com