变量声明后转到字符串 [英] Go String after variable declaration

查看:31
本文介绍了变量声明后转到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看在此处


import (
    "encoding/xml"
    "fmt"
    "os"
)

func main() {
    type Address struct {
        City, State string
    }
    type Person struct {
        XMLName   xml.Name `xml:"person"`
        Id        int      `xml:"id,attr"`
        FirstName string   `xml:"name>first"`
        LastName  string   `xml:"name>last"`
        Age       int      `xml:"age"`
        Height    float32  `xml:"height,omitempty"`
        Married   bool
        Address
        Comment string `xml:",comment"`
    }

    v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
    v.Comment = " Need more details. "
    v.Address = Address{"Hanga Roa", "Easter Island"}

    enc := xml.NewEncoder(os.Stdout)
    enc.Indent("  ", "    ")
    if err := enc.Encode(v); err != nil {
        fmt.Printf("error: %v\n", err)
    }

}

我可以在 struct Person 中理解它,它有一个名为 Id 的变量,其类型为 int ,但是那东西呢?

I can understand in the struct Person, It has a var called Id, which is of type int, but what about the stuff

xml:"person" 

?这是什么意思?谢谢.

after int? What does it mean? Thanks.

推荐答案

这是一个struct标记.图书馆使用它们来用额外的信息来注释结构域.在这种情况下,模块 encoding/xml 使用这些struct标签来表示哪些标签对应于该struct字段.

It's a struct tag. Libraries use these to annotate struct fields with extra information; in this case, the module encoding/xml uses these struct tags to denote which tags correspond to the struct fields.

这篇关于变量声明后转到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆