英文:
Golang using lowercase for first letter in Struct property
问题
我有一个关于Golang视图模板的问题,我目前在结构体属性中使用小写字母来构建结构体,然后将其作为映射传递给视图。
这是我的结构体示例:
type User struct {
uid int
username, departname string
}
然后我将结构体的集合传递给视图文件:
func (App *App) indexHander(w http.ResponseWriter, r *http.Request) {
rows, err := App.db.Query("SELECT * FROM userinfo")
checkErr(err)
t, _ := template.ParseFiles(App.folderpath + "/list.gtpl")
users := make([]User, 0) // 定义空的用户集合
for rows.Next() {
var uid int
var username string
var departname string
var created string
err = rows.Scan(&uid, &username, &departname, &created)
checkErr(err)
users = append(users, User{uid, username, departname})
}
t.Execute(w, users)
defer rows.Close()
}
以下是我的视图HTML代码:
<html>
<head>
<title></title>
</head>
<body>
<ul>
{{ range . }}
<li>{{ .username }}</li>
{{ end }}
</ul>
</body>
</html>
上述代码给我返回了空的用户数据。
但是,如果在结构体中使用首字母大写,就可以得到正确的结果:
结构体
type User struct {
Uid int
Username, Departname string
}
HTML
<html>
<head>
<title></title>
</head>
<body>
<ul>
{{ range . }}
<li>{{ .Username }}</li>
{{ end }}
</ul>
</body>
</html>
现在它可以正常工作。
有人可以解释一下这种行为吗?
英文:
I have an issue with Golang view template, I currently using lowercase in the struct properties to build the struct then passed it to the view as a map.
here is my Struct look like:
type User struct {
uid int
username, departname string
}
then I passed the collection of structs to the file view:
func (App *App) indexHander(w http.ResponseWriter, r *http.Request) {
rows, err := App.db.Query("SELECT * FROM userinfo")
checkErr(err)
t, _ := template.ParseFiles(App.folderpath + "/list.gtpl")
users := make([]User, 0) // define empty collection of users
for rows.Next() {
var uid int
var username string
var departname string
var created string
err = rows.Scan(&uid, &username, &departname, &created)
checkErr(err)
users = append(users, User{uid, username, departname})
}
t.Execute(w, users)
defer rows.Close()
}
and here is my view html code:
<html>
<head>
<title></title>
</head>
<body>
<ul>
{{ range . }}
<li>{{ .username }}</li>
{{ end }}
</ul>
</body>
</html>
Those code above gave me empty users data:
But however, using capitalize first letter in struct give me working result:
Struct
type User struct {
Uid int
Username, Departname string
}
html
<html>
<head>
<title></title>
</head>
<body>
<ul>
{{ range . }}
<li>{{ .Username }}</li>
{{ end }}
</ul>
</body>
</html>
it works now
Can somebody explain me this behavior ?
答案1
得分: 11
阅读文档这里
为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:
- 标识符名称的第一个字符是一个 Unicode 大写字母(Unicode 类别 "Lu");
- 标识符在包块中声明,或者是字段名或方法名。
所有其他标识符都不会被导出。
英文:
read the doc here
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
- the first character of the identifier's name is a Unicode upper case
letter (Unicode class "Lu"); - and the identifier is declared in thepackage block or it is a field
name or method name.
All other identifiers are not exported.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论