Golang在结构体属性的首字母上使用小写。

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

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(&quot;SELECT * FROM userinfo&quot;)
      checkErr(err)
  
      t, _ := template.ParseFiles(App.folderpath + &quot;/list.gtpl&quot;)
  
      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(&amp;uid, &amp;username, &amp;departname, &amp;created)
          checkErr(err)
          users = append(users, User{uid, username, departname})
  
      }   
  
      t.Execute(w, users)
  
      defer rows.Close()
  }

and here is my view html code:

&lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;ul&gt;
          {{ range  . }}
             &lt;li&gt;{{ .username }}&lt;/li&gt;
          {{ end }}
          &lt;/ul&gt;
      &lt;/body&gt;
  &lt;/html&gt;

Those code above gave me empty users data:
Golang在结构体属性的首字母上使用小写。

But however, using capitalize first letter in struct give me working result:

Struct

type User struct {
     Uid                  int
     Username, Departname string  
}

html

&lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;ul&gt;
          {{ range  . }}
             &lt;li&gt;{{ .Username }}&lt;/li&gt;
          {{ end }}
          &lt;/ul&gt;
      &lt;/body&gt;
  &lt;/html&gt;

it works now

Golang在结构体属性的首字母上使用小写。

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.

huangapple
  • 本文由 发表于 2017年5月1日 16:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/43716630.html
匿名

发表评论

匿名网友

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

确定