首页 > 后端开发 > Golang > 正文

Golang encoding/xml库XML数据处理方法

P粉602998670
发布: 2025-09-04 08:47:01
原创
519人浏览过
Go语言通过encoding/xml库实现XML与结构体的双向映射,利用结构体标签处理元素、属性、嵌套及混合内容,支持指针类型应对可选字段,结合omitempty、innerxml等标签提升灵活性,并通过自定义UnmarshalXML方法处理复杂场景,需注意命名空间、标签匹配、空值区分及大文件流式解析以避免常见错误。

golang encoding/xml库xml数据处理方法

在Go语言中,

encoding/xml
登录后复制
库是处理XML数据最核心、也最常用的工具。它提供了一种非常Go-idiomatic的方式,通过结构体标签(struct tags)将Go的数据结构与XML的元素和属性进行双向映射,实现XML的编码(Marshal)和解码(Unmarshal),让XML操作变得异常简洁高效。

解决方案

encoding/xml
登录后复制
的核心思想是将XML数据看作是Go结构体的序列化形式。这意味着,要处理XML,你首先需要定义一个或多个Go结构体,这些结构体的字段通过
xml
登录后复制
标签来指示它们在XML中的对应关系。

例如,假设我们有一个简单的XML结构:

<person id="123">
    <name>张三</name>
    <age>30</age>
    <email type="work">zhangsan@example.com</email>
    <skills>
        <skill>Go</skill>
        <skill>Python</skill>
    </skills>
</person>
登录后复制

我们可以这样定义Go结构体来映射它:

立即学习go语言免费学习笔记(深入)”;

package main

import (
    "encoding/xml"
    "fmt"
)

// Person 结构体映射XML的<person>根元素
type Person struct {
    XMLName xml.Name `xml:"person"` // 显式指定根元素名,可选
    ID      string   `xml:"id,attr"`  // id是属性
    Name    string   `xml:"name"`     // name是子元素
    Age     int      `xml:"age"`      // age是子元素
    Email   Email    `xml:"email"`    // Email是一个嵌套结构体
    Skills  []string `xml:"skills>skill"` // skills是父元素,skill是子元素,表示一个切片
}

// Email 结构体映射XML的<email>元素
type Email struct {
    Type  string `xml:"type,attr"` // type是属性
    Value string `xml:",chardata"` // Value获取元素内容
}

func main() {
    // 1. 从Go结构体编码为XML (Marshal)
    p := Person{
        ID:   "456",
        Name: "李四",
        Age:  25,
        Email: Email{
            Type:  "personal",
            Value: "lisi@example.com",
        },
        Skills: []string{"Java", "C++"},
    }

    output, err := xml.MarshalIndent(p, "", "  ") // 使用MarshalIndent格式化输出
    if err != nil {
        fmt.Printf("Error marshalling: %v\n", err)
        return
    }
    fmt.Println("--- Marshalled XML ---")
    fmt.Println(string(output))

    // 2. 从XML数据解码为Go结构体 (Unmarshal)
    xmlData := `
    <person id="123">
        <name>张三</name>
        <age>30</age>
        <email type="work">zhangsan@example.com</email>
        <skills>
            <skill>Go</skill>
            <skill>Python</skill>
        </skills>
    </person>`

    var decodedPerson Person
    err = xml.Unmarshal([]byte(xmlData), &decodedPerson)
    if err != nil {
        fmt.Printf("Error unmarshalling: %v\n", err)
        return
    }
    fmt.Println("\n--- Unmarshalled Person ---")
    fmt.Printf("ID: %s, Name: %s, Age: %d\n", decodedPerson.ID, decodedPerson.Name, decodedPerson.Age)
    fmt.Printf("Email: %s (Type: %s)\n", decodedPerson.Email.Value, decodedPerson.Email.Type)
    fmt.Printf("Skills: %v\n", decodedPerson.Skills)
}
登录后复制

代码中,

xml:"id,attr"
登录后复制
表示
ID
登录后复制
字段对应XML元素的
ID
登录后复制
属性;
xml:"name"
登录后复制
表示
Name
登录后复制
字段对应名为
Name
登录后复制
的子元素;
xml:",chardata"
登录后复制
用于获取元素内部的字符数据,而不是子元素。
xml:"skills>skill"
登录后复制
这种写法则巧妙地处理了嵌套列表,它会查找
skills
登录后复制
元素下的所有
skill
登录后复制
子元素,并将它们的值收集到一个字符串切片中。

Golang处理复杂XML结构时如何映射嵌套元素和属性?

处理复杂XML结构,尤其是包含多层嵌套、混合内容(元素和文本)、或者需要处理特定属性时,

encoding/xml
登录后复制
的结构体标签显得尤为重要。我个人觉得,理解它的标签语法是关键。

比如,当你有这样的XML:

<book id="bk101" available="true">
    <title lang="en">Go Programming</title>
    <author>John Doe</author>
    <chapter num="1">Introduction</chapter>
    <chapter num="2">Basics</chapter>
    <description>
        This is a great book about <highlight>Go</highlight> programming.
        It covers <topic>concurrency</topic> and <topic>web development</topic>.
    </description>
</book>
登录后复制

这里面有:

  • 根元素的属性 (
    ID
    登录后复制
    ,
    available
    登录后复制
    )。
  • 子元素的属性 (
    lang
    登录后复制
    ,
    num
    登录后复制
    )。
  • 混合内容(
    <description>
    登录后复制
    内部有文本也有子元素)。

我们的Go结构体可以这样设计:

type Book struct {
    XMLName    xml.Name  `xml:"book"`
    ID         string    `xml:"id,attr"`
    Available  bool      `xml:"available,attr"`
    Title      TitleElem `xml:"title"`
    Author     string    `xml:"author"`
    Chapters   []Chapter `xml:"chapter"`
    Description DescriptionElem `xml:"description"`
}

type TitleElem struct {
    Lang  string `xml:"lang,attr"`
    Value string `xml:",chardata"` // 获取标签内的文本
}

type Chapter struct {
    Num   int    `xml:"num,attr"`
    Value string `xml:",chardata"` // 获取<chapter>标签内的文本
}

type DescriptionElem struct {
    Content string `xml:",innerxml"` // 获取<description>内部的所有XML内容,包括子标签和文本
    // 或者如果你想更细致地解析:
    // TextParts []string   `xml:",chardata"` // 获取所有文本片段,可能不理想
    // Highlights []string  `xml:"highlight"`
    // Topics     []string  `xml:"topic"`
}</pre><div class="contentsignin">登录后复制</div></div><p>这里有几个值得注意的点:</p>
<ul>
<li>
<strong>属性映射:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:"id,attr"</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:"available,attr"</pre><div class="contentsignin">登录后复制</div></div> 清晰地将字段映射到对应元素的属性。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">bool</pre><div class="contentsignin">登录后复制</div></div> 类型会自动处理 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">"true"</pre><div class="contentsignin">登录后复制</div></div> / <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">"false"</pre><div class="contentsignin">登录后复制</div></div> 到 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">true</pre><div class="contentsignin">登录后复制</div></div> / <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">false</pre><div class="contentsignin">登录后复制</div></div> 的转换。</li>
<li>
<strong>子元素内容:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",chardata"</pre><div class="contentsignin">登录后复制</div></div> 是一个非常实用的标签,它告诉解码器将当前标签内部的纯文本内容赋给该字段。这对于像 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><title>Go Programming</title></pre><div class="contentsignin">登录后复制</div></div> 这样的简单文本元素非常有效。</li>
<li>
<strong>嵌套结构体:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">TitleElem</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Chapter</pre><div class="contentsignin">登录后复制</div></div> 都是独立的结构体,它们分别定义了自己内部的属性和文本内容。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Book</pre><div class="contentsignin">登录后复制</div></div> 结构体通过字段 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Title</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Chapters</pre><div class="contentsignin">登录后复制</div></div> 引用它们。</li>
<li>
<strong>列表处理:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Chapters []Chapter</pre><div class="contentsignin">登录后复制</div></div> 会自动收集所有同名子元素(<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><chapter></pre><div class="contentsignin">登录后复制</div></div>)并将其解码为 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Chapter</pre><div class="contentsignin">登录后复制</div></div> 结构体的一个切片。</li>
<li>
<strong>混合内容和<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div>:</strong> 对于像 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><description></pre><div class="contentsignin">登录后复制</div></div> 这样内部既有文本又有子元素的复杂情况,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",innerxml"</pre><div class="contentsignin">登录后复制</div></div> 是一个强大的工具。它会将该元素内部的所有原始XML内容(包括子标签和文本)作为字符串赋给字段。这允许你稍后手动解析这部分内容,或者直接将其展示。如果需要更精细的解析,比如提取 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><highlight></pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><topic></pre><div class="contentsignin">登录后复制</div></div>,你就需要为 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">DescriptionElem</pre><div class="contentsignin">登录后复制</div></div> 内部定义相应的字段,并让 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 去处理。但要注意,混合内容(文本和子元素交错)的自动解析往往比较棘手,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div> 提供了一个灵活的出口。</li>
</ul>
<p>通过这些标签的组合使用,几乎所有常见的XML结构都能被有效地映射到Go结构体。关键在于多实践,理解每个标签的精确含义。</p>
<h3>当XML结构不确定或包含可选字段时,Go如何灵活地解析数据?</h3>
<p>在实际项目中,XML数据源往往不那么“完美”,可能会有可选字段、字段顺序不固定,甚至某些元素可能根本不存在。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 库在处理这些不确定性方面表现得相当灵活。</p>
<p>我发现,主要有以下几种策略来应对:</p>
<ol>
<li>
<p><strong>使用指针类型处理可选字段:</strong> 这是最常见也最Go-idiomatic的方式。如果一个XML元素或属性是可选的,你可以将对应的Go结构体字段定义为指针类型,比如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*string</pre><div class="contentsignin">登录后复制</div></div>, <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*int</pre><div class="contentsignin">登录后复制</div></div>, <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*bool</pre><div class="contentsignin">登录后复制</div></div> 或 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*MyNestedStruct</pre><div class="contentsignin">登录后复制</div></div>。
当XML中存在该元素/属性时,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 会为其分配内存并解码;如果不存在,该指针字段将保持其零值 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div>。这使得你可以在解码后通过检查指针是否为 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div> 来判断原始XML中是否存在该字段。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>type Product struct {
    XMLName    xml.Name `xml:"product"`
    ID         string   `xml:"id,attr"`
    Name       string   `xml:"name"`
    Price      *float64 `xml:"price"` // price是可选的
    Description *string `xml:"description,omitempty"` // description可选,omitempty在Marshal时如果为nil则不输出
}

// 假设一个XML没有price和description
xmlNoPrice := `<product id="p001"><name>Widget</name></product>`
var p Product
xml.Unmarshal([]byte(xmlNoPrice), &p)
if p.Price == nil {
    fmt.Println("Product has no price.")
}
if p.Description == nil {
    fmt.Println("Product has no description.")
}</pre><div class="contentsignin">登录后复制</div></div></li>
<li>
<p><strong><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">omitempty</pre><div class="contentsignin">登录后复制</div></div> 标签选项:</strong> 这个标签主要用于编码(Marshal)时。当一个字段的值是其零值(例如,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">string</pre><div class="contentsignin">登录后复制</div></div> 的空字符串 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">""</pre><div class="contentsignin">登录后复制</div></div>,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">int</pre><div class="contentsignin">登录后复制</div></div> 的 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">0</pre><div class="contentsignin">登录后复制</div></div>,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">bool</pre><div class="contentsignin">登录后复制</div></div> 的 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">false</pre><div class="contentsignin">登录后复制</div></div>,或者指针的 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div>)时,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">omitempty</pre><div class="contentsignin">登录后复制</div></div> 会指示 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 在生成XML时不包含这个元素或属性。这对于生成简洁的XML非常有用,避免了输出大量空标签。</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/xiazai/code/10783">
                            <img src="https://img.php.cn/upload/webcode/000/000/009/176372460979237.jpg" alt="我要服装批发网">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/xiazai/code/10783">我要服装批发网</a>
                            <p>由逍遥网店系统修改而成,修改内容如下:前台商品可以看大图功能后台商品在线添加编辑功能 (允许UBB)破解了访问统计系统增加整合了更加强大的第三方统计系统 (IT学习者v1.6)并且更新了10月份的IP数据库。修正了后台会员订单折扣金额处理错误BUG去掉了会员折扣价这个功能,使用市场价,批发价。这样符合实际的模式,批发价非会员不可看修正了在线编辑无法使用 “代码&rdqu</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="我要服装批发网">
                                <span>0</span>
                            </div>
                        </div>
                        <a href="/xiazai/code/10783" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="我要服装批发网">
                        </a>
                    </div>
                <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>type Order struct {
    OrderID string `xml:"orderID"`
    CustomerName string `xml:"customerName"`
    SpecialInstructions string `xml:"specialInstructions,omitempty"` // 如果为空,则不输出此标签
}

order1 := Order{OrderID: "ORD123", CustomerName: "Alice"}
// Marshal order1,SpecialInstructions为空,不会出现在XML中
order2 := Order{OrderID: "ORD456", CustomerName: "Bob", SpecialInstructions: "Gift wrap"}
// Marshal order2,SpecialInstructions会出现在XML中</pre><div class="contentsignin">登录后复制</div></div></li>
<li><p><strong>使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">[]byte</pre><div class="contentsignin">登录后复制</div></div> 或 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">string</pre><div class="contentsignin">登录后复制</div></div> 配合 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div> / <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">chardata</pre><div class="contentsignin">登录后复制</div></div> 延迟解析:</strong> 如前所述,对于结构非常不确定或包含大量混合内容的元素,你可以将其映射到一个 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">string</pre><div class="contentsignin">登录后复制</div></div> 字段,并使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",innerxml"</pre><div class="contentsignin">登录后复制</div></div> 或 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",chardata"</pre><div class="contentsignin">登录后复制</div></div> 标签。这会将该元素内部的所有XML内容或纯文本内容作为原始字符串捕获。之后,你可以根据需要,使用其他XML解析库(如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 再次Unmarshal,或者 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">github.com/antchfx/xmlquery</pre><div class="contentsignin">登录后复制</div></div> 进行XPath查询)来进一步处理这部分字符串。这种方法牺牲了一些自动化,但提供了最大的灵活性。</p></li>
<li>
<p><strong>自定义 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">UnmarshalXML</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">MarshalXML</pre><div class="contentsignin">登录后复制</div></div> 方法:</strong> 对于极端复杂的或者需要特殊处理的XML结构,Go提供了 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Unmarshaler</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Marshaler</pre><div class="contentsignin">登录后复制</div></div> 接口。你可以为你的结构体实现这两个接口,完全控制XML的解码和编码过程。这给了你最大的自由度,可以处理任何非标准或高度定制化的XML格式,例如:</p>
<ul>
<li>根据某个属性的值来决定解析哪个子结构。</li>
<li>处理XML中同一层级出现多个同名但含义不同的元素。</li>
<li>执行复杂的类型转换或数据验证。</li>
</ul>
<p>虽然这种方式需要编写更多的代码,但它提供了一个“逃生舱口”,确保你总能处理最棘手的XML。</p>
</li>
</ol>
<p>通过结合这些方法,我们可以构建出既健壮又灵活的Go程序,来应对各种复杂和不确定的XML数据源。</p>
<h3>在Golang中处理XML时,常见的陷阱和错误有哪些?如何避免?</h3>
<p>在使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 库时,我确实遇到过一些“坑”,这些问题往往不是代码逻辑错误,而是对XML结构和Go映射规则理解不足导致的。避免这些陷阱能大大提高开发效率。</p>
<ol>
<li>
<p><strong>XML标签名称与Go字段名不匹配:</strong></p>
<ul>
<li>
<strong>陷阱:</strong> Go结构体字段名默认会根据大小写转换为XML标签名。例如,<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">FieldName</pre><div class="contentsignin">登录后复制</div></div> 会尝试匹配 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><FieldName></pre><div class="contentsignin">登录后复制</div></div>。但如果XML标签是 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><field_name></pre><div class="contentsignin">登录后复制</div></div> 或 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><field-name></pre><div class="contentsignin">登录后复制</div></div>,直接映射就会失败。</li>
<li>
<strong>避免:</strong> 总是显式使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:"tag_name"</pre><div class="contentsignin">登录后复制</div></div> 标签来指定XML元素或属性的精确名称。这不仅能解决不匹配问题,还能提高代码的可读性和维护性。对于属性,记住要加上 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">,attr</pre><div class="contentsignin">登录后复制</div></div>,如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:"id,attr"</pre><div class="contentsignin">登录后复制</div></div>。</li>
</ul>
</li>
<li>
<p><strong>忽略XML命名空间(Namespace):</strong></p>
<ul>
<li>
<strong>陷阱:</strong> XML命名空间是用来避免元素名冲突的,如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><soap:Envelope></pre><div class="contentsignin">登录后复制</div></div>。如果你的XML使用了命名空间,而Go结构体没有正确处理,通常会解析失败或者只解析到没有命名空间的元素。</li>
<li>
<strong>避免:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 可以处理命名空间,但需要你在结构体字段的 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml</pre><div class="contentsignin">登录后复制</div></div> 标签中包含命名空间前缀,或者更常见的是,在 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">XMLName</pre><div class="contentsignin">登录后复制</div></div> 字段中指定命名空间。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>type SOAPEnvelope struct {
    XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ soap:Envelope"`
    Body    SOAPBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}
// 或者,如果命名空间在父元素定义,子元素可以只用本地名
type Book struct {
    XMLName xml.Name `xml:"urn:book Book"` // 根元素指定命名空间
    Title   string   `xml:"Title"` // 子元素可以直接使用本地名
}</pre><div class="contentsignin">登录后复制</div></div><p>理解命名空间的工作方式,并在需要时显式指定,是关键。</p>
</li>
</ul>
</li>
<li>
<p><strong>误用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">chardata</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div>:</strong></p>
<ul>
<li>
<strong>陷阱:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",chardata"</pre><div class="contentsignin">登录后复制</div></div> 只捕获元素内部的纯文本内容,会忽略所有子元素。而 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml:",innerxml"</pre><div class="contentsignin">登录后复制</div></div> 捕获元素内部的原始XML字符串,包括所有子元素和文本。如果期望捕获子元素内容却用了 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">chardata</pre><div class="contentsignin">登录后复制</div></div>,或者期望纯文本却用了 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div>,都会导致数据丢失或格式不符。</li>
<li>
<strong>避免:</strong> 仔细区分这两种标签的用途。当元素只包含文本时,用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">chardata</pre><div class="contentsignin">登录后复制</div></div>。当元素内部有混合内容(文本和子元素)且你需要完整保留内部结构时,用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">innerxml</pre><div class="contentsignin">登录后复制</div></div>。如果需要解析内部的特定子元素,就应该定义嵌套结构体而不是使用这两个标签。</li>
</ul>
</li>
<li>
<p><strong>处理空元素与零值:</strong></p>
<ul>
<li>
<strong>陷阱:</strong> XML中 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><tag></tag></pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"><tag/></pre><div class="contentsignin">登录后复制</div></div> 都表示空元素。Go在Unmarshal时,会将它们映射到对应字段的零值(例如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">""</pre><div class="contentsignin">登录后复制</div></div> for <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">string</pre><div class="contentsignin">登录后复制</div></div>, <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">0</pre><div class="contentsignin">登录后复制</div></div> for <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">int</pre><div class="contentsignin">登录后复制</div></div>, <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">false</pre><div class="contentsignin">登录后复制</div></div> for <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">bool</pre><div class="contentsignin">登录后复制</div></div>)。如果字段是指针类型,它们会被设为 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div>。但有时你可能需要区分“字段不存在”和“字段存在但为空”。</li>
<li>
<strong>避免:</strong> 对于需要区分“不存在”和“空值”的情况,使用指针类型(如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*string</pre><div class="contentsignin">登录后复制</div></div>)是最佳实践。如果指针为 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div>,则表示XML中没有该元素;如果指针非 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">nil</pre><div class="contentsignin">登录后复制</div></div> 但其指向的值是零值(如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">*s = ""</pre><div class="contentsignin">登录后复制</div></div>),则表示XML中存在该元素但为空。</li>
</ul>
</li>
<li>
<p><strong>Unmarshal时忘记传递指针:</strong></p>
<ul>
<li>
<strong>陷阱:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Unmarshal</pre><div class="contentsignin">登录后复制</div></div> 的第二个参数必须是一个指向结构体的指针,例如 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Unmarshal(data, &myStruct)</pre><div class="contentsignin">登录后复制</div></div>。如果传递的是值类型(<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">myStruct</pre><div class="contentsignin">登录后复制</div></div> 而非 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">&myStruct</pre><div class="contentsignin">登录后复制</div></div>),Go编译器通常不会报错,但数据不会被正确填充。</li>
<li>
<strong>避免:</strong> 养成习惯,凡是需要修改传入参数内容的函数(如解码操作),其参数通常都需要是指针。</li>
</ul>
</li>
<li>
<p><strong>错误处理不足:</strong></p>
<ul>
<li>
<strong>陷阱:</strong> XML解析过程中可能会出现多种错误,例如XML格式不正确、编码问题、或者与结构体映射不匹配。如果不对 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Marshal</pre><div class="contentsignin">登录后复制</div></div> 和 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Unmarshal</pre><div class="contentsignin">登录后复制</div></div> 返回的错误进行检查,程序可能会在运行时崩溃或产生不可预测的结果。</li>
<li>
<strong>避免:</strong> 始终检查 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">err</pre><div class="contentsignin">登录后复制</div></div> 返回值。一个健壮的程序应该能够优雅地处理这些错误,例如记录日志、返回错误信息给用户,或者使用默认值。</li>
</ul>
</li>
<li>
<p><strong>性能考虑(针对大文件):</strong></p>
<ul>
<li>
<strong>陷阱:</strong> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">encoding/xml</pre><div class="contentsignin">登录后复制</div></div> 在处理非常大的XML文件时,会一次性将整个文件读入内存进行解析。这可能导致内存占用过高,甚至OOM(Out Of Memory)。</li>
<li>
<strong>避免:</strong> 对于GB级别的大型XML文件,不建议直接使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Unmarshal</pre><div class="contentsignin">登录后复制</div></div>。这时,应该考虑使用 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Decoder</pre><div class="contentsignin">登录后复制</div></div> 配合 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">Token()</pre><div class="contentsignin">登录后复制</div></div> 方法进行流式解析。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">xml.Decoder</pre><div class="contentsignin">登录后复制</div></div> 允许你逐个读取XML的Token(开始标签、结束标签、字符数据等),从而在不将整个文件加载到内存的情况下处理数据。这虽然增加了代码复杂性,但对内存效率至关重要。</li>
</ul>
</li>
</ol>
<p>通过提前了解这些常见问题,并在编码时多加注意,可以有效减少调试时间,并构建出更稳定、更健壮的Go XML处理应用。</p><p>以上就是Golang encoding/xml库XML数据处理方法的详细内容,更多请关注php中文网其它相关文章!</p>                <div class="wzconBq" style="display: inline-flex;">
                    <span>相关标签:</span>
                    <div class="wzcbqd" style="display:flex;">
                        <a onclick="hits_log(2,'www',this);" href-data="/zt/15730.html" target="_blank">python</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/15731.html" target="_blank">java</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/15841.html" target="_blank">git</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/15863.html" target="_blank">go</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/15997.html" target="_blank">github</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/16009.html" target="_blank">golang</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/16043.html" target="_blank">go语言</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/16887.html" target="_blank">工具</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/17539.html" target="_blank">ai</a> <a onclick="hits_log(2,'www',this);" href-data="/zt/17603.html" target="_blank">c++</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=golang" target="_blank">golang</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=String" target="_blank">String</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=for" target="_blank">for</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=命名空间" target="_blank">命名空间</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=xml" target="_blank">xml</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=Token" target="_blank">Token</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=字符串" target="_blank">字符串</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=结构体" target="_blank">结构体</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=bool" target="_blank">bool</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=int" target="_blank">int</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=指针" target="_blank">指针</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=数据结构" target="_blank">数据结构</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=接口" target="_blank">接口</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=值类型" target="_blank">值类型</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=指针类型" target="_blank">指针类型</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=Struct" target="_blank">Struct</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=Namespace" target="_blank">Namespace</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=Go语言" target="_blank">Go语言</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=切片" target="_blank">切片</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=nil" target="_blank">nil</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=类型转换" target="_blank">类型转换</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=github" target="_blank">github</a> <a onclick="hits_log(2,'www',this);" href-data="/search?q=自动化" target="_blank">自动化</a>                       
                    </div>
                   
                </div>
                
                                <div class="everysee">
                    <h2>大家都在看:</h2>
                    <div>
                                        <a target="_blank"  href="/faq/1840835.html" title="Go encoding/xml处理同名异命名空间XML元素的挑战与策略">Go encoding/xml处理同名异命名空间XML元素的挑战与策略</a>
                                        <a target="_blank"  href="/faq/1840814.html" title="使用Go语言通过Web界面实时展示图像数据">使用Go语言通过Web界面实时展示图像数据</a>
                                        <a target="_blank"  href="/faq/1840742.html" title="Go语言并发编程:理解Map中Slice值的数据竞争与深拷贝实践">Go语言并发编程:理解Map中Slice值的数据竞争与深拷贝实践</a>
                                        <a target="_blank"  href="/faq/1840593.html" title="Go语言图像处理与Web可视化教程:实时显示图像结果">Go语言图像处理与Web可视化教程:实时显示图像结果</a>
                                        <a target="_blank"  href="/faq/1838999.html" title="JavaScript位移操作的陷阱:如何正确模拟8位字节移位">JavaScript位移操作的陷阱:如何正确模拟8位字节移位</a>
                                        </div>
                </div>
                          
                </div>
                            </div>
                
            <!-- <div class="ask_line-container" >
                <div class="ask_line"></div>
                <button type="button" class="ask_text test-iframe-handle">
                没有解决问题?点击使用智能助手
                </button>
                <div class="ask_line"></div>
            </div> -->
                        <div class="community flexRow newcommunity">
                    <div class="comleft flexRow newcomlimg">
                        <a target="_blank"  class="newcomlimga" target="_blank" rel="nofollow" href="https://pan.quark.cn/s/d15282cdb2c0" title="最佳 Windows 性能的顶级免费优化软件" >
                            <img src="https://img.php.cn/teacher/course/20240902/cc89be6a0150bc48ad7781bd49eed9bf.png" class="comlimg newcomlimg" alt="最佳 Windows 性能的顶级免费优化软件">
                        </a>
                        <div class="comldiv flexColumn newcomldiv">
                            <a target="_blank"  class="comldup newcomldup" target="_blank" rel="nofollow" title="最佳 Windows 性能的顶级免费优化软件" href="https://pan.quark.cn/s/d15282cdb2c0">最佳 Windows 性能的顶级免费优化软件</a>
                            <p class="comlddown newcomlddown">每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。</p>
                        </div>
                    </div>
                    <a target="_blank"  class="comright flexRow newcomright" target="_blank" rel="nofollow" href="https://pan.quark.cn/s/d15282cdb2c0" title="最佳 Windows 性能的顶级免费优化软件">
                        下载
                    </a>
                </div>
            
          
			
            <div  class="wzconlabels">
                
                                <div style="display: inline-flex;float: right; color:#333333;">来源:php中文网</div>
                            </div>


            <div class="wzconFx">
                <a target="_blank"  class="wzcf-sc articleICollection " data-id="1495325">
                                            <img src="/static/lhimages/shoucang_2x.png">
                        <span>收藏</span>
                                    </a>

                <a target="_blank"   class="wzcf-dz articlegoodICollection " data-id="1495325">
                                            <img src="/static/images/images/icon37.png">
                        <span>点赞</span>
                                    </a>
            </div>



            <div class="wzconOtherwz">
                                    <a target="_blank"  href="/faq/1495305.html">
                        <span>上一篇:Golang微服务网关实现与请求转发</span>
                    </a>
                                    <a target="_blank"  href="/faq/1495331.html">
                        <span>下一篇:Golang text/template库文本模板生成与使用</span>
                    </a>
                            </div>
            <div class="wzconShengming">
                <img src="/static/images/images/benzhanshengming.png" />
                <div>本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn</div>
            </div>

                       <!-- PC-文章底部 -->
                        <div class="wzconZzwz">
                <div class="wzconZzwztitle">作者最新文章</div>
                <ul>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680768.html" title="phpcn{$vo.title}">百度浏览器网页背景显示异常怎么办 百度浏览器页面背景显示修复方法</a>
                            </div>
                            <div>2025-11-03 10:03:33</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680777.html" title="phpcn{$vo.title}">在Java中如何理解继承与多态的关系_Java继承多态应用技巧</a>
                            </div>
                            <div>2025-11-03 10:05:22</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680794.html" title="phpcn{$vo.title}">如何在CSS中实现响应式导航栏布局_Flex与Grid结合应用</a>
                            </div>
                            <div>2025-11-03 10:10:02</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680796.html" title="phpcn{$vo.title}">Safari浏览器网页显示异常怎么办 Safari浏览器页面布局错乱修复方法</a>
                            </div>
                            <div>2025-11-03 10:11:06</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680822.html" title="phpcn{$vo.title}">俄罗斯浏览器​Яндекс中文版入口 Яндекс官方网页版登录地址</a>
                            </div>
                            <div>2025-11-03 10:16:20</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680825.html" title="phpcn{$vo.title}">美团外卖双十一优惠券入口在哪详细教程</a>
                            </div>
                            <div>2025-11-03 10:17:02</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680828.html" title="phpcn{$vo.title}">如何在Golang中实现容器健康检查逻辑</a>
                            </div>
                            <div>2025-11-03 10:17:17</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680839.html" title="phpcn{$vo.title}">如何在Golang中实现Web接口统一返回结构</a>
                            </div>
                            <div>2025-11-03 10:19:21</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680846.html" title="phpcn{$vo.title}">夸克浏览器下载任务无法暂停怎么办 夸克浏览器下载控制方法</a>
                            </div>
                            <div>2025-11-03 10:21:02</div>
                        </li>
                                            <li>
                            <div class="wzczzwzli">
                                <span class="layui-badge-dots"></span>
                                <a target="_blank"  target="_blank" href="/faq/1680851.html" title="phpcn{$vo.title}">微信聊天记录无法导出怎么办 微信聊天导出与备份方法</a>
                            </div>
                            <div>2025-11-03 10:21:47</div>
                        </li>
                                    </ul>
            </div>
                        <div class="wzconZzwz">
                <div class="wzconZzwztitle">最新问题</div>
                <div class="wdsyContent">
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1876030.html"  target="_blank" title="如何在Golang中处理函数返回指针值_确保返回安全地址" class="wdcdcTitle">如何在Golang中处理函数返回指针值_确保返回安全地址</a>
                            <a target="_blank"  href="/faq/1876030.html" class="wdcdcCons">Go中返回指针安全的前提是所指内存有效:堆分配、全局变量、可达切片首元素或有效指针接收者;避免返回未逃逸局部变量地址(编译器通常自动处理)、C内存或已释放资源指针。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 08:14:03</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">179</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875930.html"  target="_blank" title="如何使用Golang反射简化通用拦截器开发_Golang reflect通用拦截逻辑解析" class="wdcdcTitle">如何使用Golang反射简化通用拦截器开发_Golang reflect通用拦截逻辑解析</a>
                            <a target="_blank"  href="/faq/1875930.html" class="wdcdcCons">Go通用拦截器核心是运行时识别方法签名、动态调用与统一处理,需满足导出方法、指针接收者,并用reflect.Value.MethodByName安全调用,配合Call执行、panic捕获及日志/耗时/错误包装。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 06:59:23</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">397</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875924.html"  target="_blank" title="如何使用Golang按行读取大文件_Golangbufio Scanner文件行读取技巧汇总" class="wdcdcTitle">如何使用Golang按行读取大文件_Golangbufio Scanner文件行读取技巧汇总</a>
                            <a target="_blank"  href="/faq/1875924.html" class="wdcdcCons">bufio.Scanner按行读取大文件最常用且稳妥,但默认64KB行长限制易触发ErrTooLong,需调用scanner.Buffer()自定义缓冲区大小。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 06:47:29</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">601</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875892.html"  target="_blank" title="如何使用Golang理解指针与引用传递_避免不必要的内存拷贝" class="wdcdcTitle">如何使用Golang理解指针与引用传递_避免不必要的内存拷贝</a>
                            <a target="_blank"  href="/faq/1875892.html" class="wdcdcCons">Go语言只有值传递,但可通过指针模拟引用行为;传指针仅复制地址(8字节),避免大对象拷贝,且能修改原值;需根据是否需修改、拷贝成本及方法接收者需求决定是否用指针。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 05:43:10</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">566</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875889.html"  target="_blank" title="如何用Golang实现异步事件处理_Golang 异步事件处理实践" class="wdcdcTitle">如何用Golang实现异步事件处理_Golang 异步事件处理实践</a>
                            <a target="_blank"  href="/faq/1875889.html" class="wdcdcCons">使用Golang的goroutine和channel可构建高效异步事件处理器;2.定义Event结构体与EventHandler函数类型实现事件驱动;3.EventBus通过channel非阻塞分发事件并支持动态订阅;4.Publish发送事件不阻塞主流程,Subscribe注册对应处理器;5.实际示例展示多处理器并行执行;6.需注意错误处理、资源控制、优雅关闭与性能监控。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 05:37:09</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">635</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875880.html"  target="_blank" title="如何使用Golang安装并配置Delve调试器_支持断点和代码单步调试" class="wdcdcTitle">如何使用Golang安装并配置Delve调试器_支持断点和代码单步调试</a>
                            <a target="_blank"  href="/faq/1875880.html" class="wdcdcCons">直接用goinstall安装Delve即可支持断点和单步调试,无需修改源码;推荐命令为goinstallgithub.com/go-delve/delve/cmd/dlv@latest,安装后通过dlvdebug或VSCode配置launch.json调试。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 05:15:13</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">954</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875857.html"  target="_blank" title="Golang如何使用gRPC实现流控与限流_Golang gRPC流控限流机制实践" class="wdcdcTitle">Golang如何使用gRPC实现流控与限流_Golang gRPC流控限流机制实践</a>
                            <a target="_blank"  href="/faq/1875857.html" class="wdcdcCons">gRPC基于HTTP/2提供流控机制,通过InitialWindowSize等参数配置窗口大小,结合拦截器与rate包实现服务级限流,使用令牌桶控制请求速率,支持单机及Redis分布式限流,保障微服务稳定性。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 04:29:31</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">149</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875843.html"  target="_blank" title="如何在Golang中处理网络连接异常_重连和错误日志记录" class="wdcdcTitle">如何在Golang中处理网络连接异常_重连和错误日志记录</a>
                            <a target="_blank"  href="/faq/1875843.html" class="wdcdcCons">Go语言网络连接异常处理需分离逻辑、指数退避重连、结构化日志;封装带重试的dial函数,用context控制生命周期,区分临时性(可重试)与永久性错误(立即停止)。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-22 03:59:29</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">182</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875261.html"  target="_blank" title="如何使用Golang实现微服务鉴权_基于Token或JWT控制访问" class="wdcdcTitle">如何使用Golang实现微服务鉴权_基于Token或JWT控制访问</a>
                            <a target="_blank"  href="/faq/1875261.html" class="wdcdcCons">Golang微服务鉴权需统一在网关或中间件校验JWT,涵盖签名、时效、身份提取与权限比对;配合短AccessToken+长RefreshToken(Redis存储)实现退出与续期;通过角色/权限字段做细粒度授权,敏感操作需二次验证;跨服务调用应透传可信内部token或借助ServiceMesh。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-21 21:07:02</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">900</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                        <div class="wdsyConDiv flexRow wdsyConDiv1">
                        <div class="wdcdContent flexColumn">
                            <a target="_blank"  href="/faq/1875252.html"  target="_blank" title="为什么Go中推荐逐层返回error_Go错误传播机制解析" class="wdcdcTitle">为什么Go中推荐逐层返回error_Go错误传播机制解析</a>
                            <a target="_blank"  href="/faq/1875252.html" class="wdcdcCons">Go中推荐逐层返回error是为了保持错误上下文、明确责任边界、避免掩盖真实问题;error是普通返回值,需显式传递,通过%w包装形成可追溯的错误链,并将处理决策权交给更了解业务场景的上层。</a>
                            <div class="wdcdcInfo flexRow">
                                <div class="wdcdcileft">
                                    <span class="wdcdciSpan">2025-12-21 21:03:36</span>
                                </div>
                                <div class="wdcdciright flexRow">
                                    <a target="_blank"  class="wdcdcirwatch flexRow"><img src="/static/images/images/icon43.png"  class="wdcdcirwatchi">758</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="wdsyConLine wdsyConLine2"></div>
                                    </div>
            </div>
            <div class="wzconZt" >
                <div class="wzczt-title">
                    <div>相关专题</div>
                    <a target="_blank"  href="/faq/zt" target="_blank">更多>
                    </a>
                </div>
                <div class="wzcttlist">
                    <ul>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangrhdybl"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202401/27/2024012711462553414.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangrhdybl" class="title-a-spanl"><span>golang如何定义变量</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangynxsjzh"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202401/27/2024012711552254696.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangynxsjzh" class="title-a-spanl"><span>golang有哪些数据转换方法</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangcykynx"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202402/06/2024020614491669087.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangcykynx" class="title-a-spanl"><span>golang常用库有哪些</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golanggolangd"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202403/05/2024030517354684603.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golanggolangd" class="title-a-spanl"><span>golang和python的区别是什么</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangsmfdm"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202405/21/2024052115135391298.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangsmfdm" class="title-a-spanl"><span>golang是免费的吗</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangjgtdq"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202506/09/2025060915221559704.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangjgtdq" class="title-a-spanl"><span>golang结构体相关大全</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangpdffdq"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202506/10/2025061017002841902.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangpdffdq" class="title-a-spanl"><span>golang相关判断方法</span> </a>
                        </li>
                                                <li class="ul-li">
                            <a target="_blank"  target="_blank" href="/faq/golangszsyff"><img onerror="this.onerror=''; this.src='/static/images/default1.png'" src="https://img.php.cn/upload/subject/202506/17/2025061715093054115.jpg?x-oss-process=image/resize,m_fill,h_145,w_220" /> </a>
                            <a target="_blank"  target="_blank" href="/faq/golangszsyff" class="title-a-spanl"><span>golang数组使用方法</span> </a>
                        </li>
                                            </ul>
                </div>
            </div>
<div class="wzcongg"><script type="text/javascript" src="https://teacher.php.cn/php/NzIzNmE5NjBlOTgwNWZlNTMzN2E0MGEzNmU3NjM3NmI6Og==" ></script></div>
        </div>
    </div>
    <div class="phpwzright">
            <div class="wzrOne" style="margin-bottom:31px; padding:0px; width: 366px;">
        <script type="text/javascript" src="https://teacher.php.cn/php/N2Q0ODk3NTAwZTFmODQ1NGM4Y2VjYzQzZTVkOTI5NDk6Og==" ></script>
        <!-- <script type="text/javascript" smua="d=p&s=b&u=u2839468&w=366&h=270" src="https://www.nkscdn.com/smu/o.js"></script> -->
        </div>
		        <div class="wzrOne">
            <div class="wzroTitle">热门推荐</div>
            <div class="wzroList">
                <ul>
                                        
                            <li>
                                <div class="wzczzwzli">
                                    <span class="layui-badge-dots wzrolr"></span>
                                    <a target="_blank"  style="height: auto;" href="/faq/1869734.html">Golang math.Abs怎么用 Golang标准库绝对值函数详解</a>
                                </div>
                            </li>
                                        
                            <li>
                                <div class="wzczzwzli">
                                    <span class="layui-badge-dots wzrolr"></span>
                                    <a target="_blank"  style="height: auto;" href="/faq/1844518.html">Go语言中布尔类型异或操作的实现策略</a>
                                </div>
                            </li>
                                        
                            <li>
                                <div class="wzczzwzli">
                                    <span class="layui-badge-dots wzrolr"></span>
                                    <a target="_blank"  style="height: auto;" href="/faq/1843191.html">如何在Go语言中避免url.ResolveReference移除URL末尾斜杠</a>
                                </div>
                            </li>
                                        
                            <li>
                                <div class="wzczzwzli">
                                    <span class="layui-badge-dots wzrolr"></span>
                                    <a target="_blank"  style="height: auto;" href="/faq/1842599.html">Go encoding/xml 包处理命名空间与同名元素冲突:深入解析与实践</a>
                                </div>
                            </li>
                                        
                            <li>
                                <div class="wzczzwzli">
                                    <span class="layui-badge-dots wzrolr"></span>
                                    <a target="_blank"  style="height: auto;" href="/faq/1842564.html">Go encoding/xml 处理 XML 命名空间冲突及解决方案</a>
                                </div>
                            </li>
                    
                </ul>
            </div>
            
        </div>
        <div class="wzrTwo">
                    </div>
		<div class="wzrTwo">
                <div style="position: relative;"><a target="_blank"  class="" href="https://teacher.php.cn/jump/67" title="开源免费商场系统" rel="nofollow" target="_blank"><img style="width: 100%; " src="https://img.php.cn/teacher/course/20220930/8ef7a4a308a22ece023e77e5428c0e25.png" alt="开源免费商场系统"></a><span style="position: absolute;right: 5px;border: 1px solid #333;padding: 2px;color: #333;line-height: 14px;font-size: 12px;bottom: 5px;">广告</span></div>
        		
		</div>
        <div class="wzrThree">
            <div class="wzrthree-title">
                <div>热门教程</div>
                <a target="_blank"  target="_blank" href="https://www.php.cn/k.html">更多>
                </a>
            </div>
            <div class="wzrthreelist">
                <div class="wzrthreeTab">
                    <div class="check tabdiv" data-id="one">相关推荐 <div></div></div>
                    <div class="tabdiv" data-id="two">热门推荐<div></div></div>
                    <div class="tabdiv" data-id="three">最新课程<div></div></div>
                </div>
                <ul class="one">
                <script type="text/javascript" src="https://teacher.php.cn/php/MTJjOWU0YjVmMmE1MzI1OTgyNzRlYmJmYjE0MmZkNWY6Og==" ></script>
                                                <li>
                                <a target="_blank"  target="_blank" href="/course/1688.html" title="Django 教程" class="wzrthreelaimg">
                                    <img src="https://img.php.cn/upload/course/000/000/090/68a6fd2c0a705569.jpeg" alt="Django 教程"/>
                                </a>
                                <div class="wzrthree-right">
                                    <a target="_blank"  target="_blank" href="/course/1688.html">Django 教程</a>
                                    <div class="wzrthreerb">
                                        <div >23014次学习</div>
                                                                                    <a target="_blank"  class="courseICollection" data-id="1688"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                            </div>
                                </div>
                            </li>
                                                        <li>
                                <a target="_blank"  target="_blank" href="/course/1686.html" title="MySQL 教程" class="wzrthreelaimg">
                                    <img src="https://img.php.cn/upload/course/000/000/090/68a58ba6b8207458.png" alt="MySQL 教程"/>
                                </a>
                                <div class="wzrthree-right">
                                    <a target="_blank"  target="_blank" href="/course/1686.html">MySQL 教程</a>
                                    <div class="wzrthreerb">
                                        <div >13409次学习</div>
                                                                                    <a target="_blank"  class="courseICollection" data-id="1686"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                            </div>
                                </div>
                            </li>
                                                        <li>
                                <a target="_blank"  target="_blank" href="/course/1684.html" title="SciPy 教程" class="wzrthreelaimg">
                                    <img src="https://img.php.cn/upload/course/000/000/090/689da63e955bb889.png" alt="SciPy 教程"/>
                                </a>
                                <div class="wzrthree-right">
                                    <a target="_blank"  target="_blank" href="/course/1684.html">SciPy 教程</a>
                                    <div class="wzrthreerb">
                                        <div >8587次学习</div>
                                                                                    <a target="_blank"  class="courseICollection" data-id="1684"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                            </div>
                                </div>
                            </li>
                                            </ul>
                <ul class="two" style="display: none;">
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1656.html" title="JavaScript ES5基础线上课程教学" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/081/6862652adafef801.png" alt="JavaScript ES5基础线上课程教学"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1656.html">JavaScript ES5基础线上课程教学</a>
                                <div class="wzrthreerb">
                                    <div >68781次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1656"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/812.html" title="最新ThinkPHP 5.1全球首发视频教程(60天成就PHP大牛线上培训班课)" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/041/620debc3eab3f377.jpg" alt="最新ThinkPHP 5.1全球首发视频教程(60天成就PHP大牛线上培训班课)"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/812.html">最新ThinkPHP 5.1全球首发视频教程(60天成就PHP大牛线上培训班课)</a>
                                <div class="wzrthreerb">
                                    <div >1503919次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="812"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/639.html" title="phpStudy极速入门视频教程" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/068/62611ef88fcec821.jpg" alt="phpStudy极速入门视频教程"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/639.html">phpStudy极速入门视频教程</a>
                                <div class="wzrthreerb">
                                    <div >532169次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="639"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/379.html" title="独孤九贱(4)_PHP视频教程" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/001/5d1c6dfc9eb09885.jpg" alt="独孤九贱(4)_PHP视频教程"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/379.html">独孤九贱(4)_PHP视频教程</a>
                                <div class="wzrthreerb">
                                    <div >1257760次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="379"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/801.html" title="PHP实战天龙八部之仿爱奇艺电影网站" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/001/5d2426f409839992.jpg" alt="PHP实战天龙八部之仿爱奇艺电影网站"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/801.html">PHP实战天龙八部之仿爱奇艺电影网站</a>
                                <div class="wzrthreerb">
                                    <div >773940次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="801"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                    </ul>
                <ul class="three" style="display: none;">
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1696.html" title="最新Python教程 从入门到精通" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/081/68c135bb72783194.png" alt="最新Python教程 从入门到精通"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1696.html">最新Python教程 从入门到精通</a>
                                <div class="wzrthreerb">
                                    <div >5393次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1696"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1656.html" title="JavaScript ES5基础线上课程教学" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/081/6862652adafef801.png" alt="JavaScript ES5基础线上课程教学"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1656.html">JavaScript ES5基础线上课程教学</a>
                                <div class="wzrthreerb">
                                    <div >68781次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1656"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1655.html" title="PHP新手语法线上课程教学" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/081/684a8c23d811b293.png" alt="PHP新手语法线上课程教学"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1655.html">PHP新手语法线上课程教学</a>
                                <div class="wzrthreerb">
                                    <div >7837次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1655"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1654.html" title="支付宝沙箱支付(个人也能用的支付)" class="wzrthreelaimg">
                                <img src="https://img.php.cn/teacher/course/20240819/172406094466c31510e008b.jpg" alt="支付宝沙箱支付(个人也能用的支付)"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1654.html">支付宝沙箱支付(个人也能用的支付)</a>
                                <div class="wzrthreerb">
                                    <div >5024次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1654"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                            <li>
                            <a target="_blank"  target="_blank" href="/course/1650.html" title="麻省理工大佬Python课程" class="wzrthreelaimg">
                                <img src="https://img.php.cn/upload/course/000/000/067/66592dcfeb1b4698.png" alt="麻省理工大佬Python课程"/>
                            </a>
                            <div class="wzrthree-right">
                                <a target="_blank"  target="_blank" href="/course/1650.html">麻省理工大佬Python课程</a>
                                <div class="wzrthreerb">
                                    <div >46882次学习</div>
                                                                            <a target="_blank"  class="courseICollection" data-id="1650"><img src="/static/images/images/icon-small-nocollect.png" class="nofollow">收藏</a>
                                                                    </div>
                            </div>
                        </li>
                                    </ul>
            </div>
            <script>
                $('.wzrthreeTab>div').click(function(e){
                    $('.wzrthreeTab>div').removeClass('check')
                    $(this).addClass('check')
                    $('.wzrthreelist>ul').css('display','none')
                    $('.'+e.currentTarget.dataset.id).show()
                })
            </script>
        </div>


        <script>
                $(document).ready(function(){
                    var sjyx_banSwiper = new Swiper(".sjyx_banSwiperwz",{
                        speed:1000,
                        autoplay:{
                            delay:3500,
                            disableOnInteraction: false,
                        },
                        pagination:{
                            el:'.sjyx_banSwiperwz .swiper-pagination',
                            clickable :false,
                        },
                        loop:true
                    });
                    loading();
                    
                })


                function loading(reloading=false){
                    if(reloading){
                        $("#ai_doubao2_3_wait_right").show();
                        $("#ai_doubao2_3_wait_left").show();
                        $("#doubao_error_right").hide();
                        $("#doubao_error_left").hide();
                    }
                    $.ajax({
                        url:'/index.php/api/process?sign=&id=1495325&time='+Date.now(),
                        dataType:'json',
                        async:true,
                        headers: {
                            "X-Requested-With": "XMLHttpRequest" // 标记为异步请求
                        },
                        type:'post',
                        success:function(result){
                            if(result.code!=1){
                                $("#doubao_error_right").show();
                                $("#ai_doubao2_3_wait_right").hide();
                                $("#doubao_error_left").show();
                                $("#ai_doubao2_3_wait_left").hide();
                            }else{
                                $("#ai_doubao2_3_wait_right").hide();
                                $("#ai_doubao2_3_wait_left").hide();
                                let doubao_answer = `<p>`+result.data.content+`</p>`;
                                $("#doubao_answer_right").html(doubao_answer);
                                $("#doubao_answer_left").html(doubao_answer);
                                let doubao_titles = '';
                                if(result.data.title){
                                    $.each(result.data.title,function(k,v){
                                        doubao_titles+=`<div class="ai_doubao2_2s"><a target="_blank"  rel="nofollow" target="_blank" href="https://doubao.com/chat/new-thread?flow_tracking_source=360_php&channel=360_php_abstract&source=360_db_php_abstract&keywordid=`+"1495325"+`&query=参考https://www.php.cn`+"/faq/1495325.html的内容,"+encodeURIComponent(v)+`" title="`+v+`"><p><img src="/static/images/doubao_yellowstar.png" alt=""> `+v+`</p></a></div>`;
                                    })
                                    
                                }

                                yigeyigezichulai(ai_doubao_titles_show,doubao_titles);
                            }
                        }
                    })
                }

                function ai_doubao_titles_show(str){
                    $("#ai_doubao_titles_right").html(str);
                    $("#ai_doubao_titles_left").html(str);
                }
                function yigeyigezichulai(callback,str){
                    const textElements = document.querySelectorAll('.yigeyigezichulai p');
                    textElements.forEach(textElement => {
                        const originalHTML = textElement.innerHTML; 
                        const tempDiv = document.createElement('div'); 
                        tempDiv.innerHTML = originalHTML;

                        const fragments = [];
                        Array.from(tempDiv.childNodes).forEach(node => {
                            if (node.nodeType === Node.TEXT_NODE) {
                                fragments.push(...node.textContent.split(''));
                            } else {
                                fragments.push(node.outerHTML);
                            }
                        });

                        textElement.innerHTML = ''; 
                        let index = 0;

                        const interval = setInterval(() => {
                            if (index < fragments.length) {
                                const fragment = fragments[index];
                                
                                
                                if (fragment.startsWith('<')) {
                                    textElement.innerHTML += fragment;
                                } else {
                                    textElement.innerHTML += fragment;
                                }
                            } else {
                                clearInterval(interval);
                                callback(str);
                            }
                            index++;
                        }, 25); // 每 100 毫秒显示一个片段
                    });
                }
                            

                // 豆包等待动画
                const containers = document.querySelectorAll('.ai_doubao2_3_wait') || [];
                    if (containers.length > 0) {
                        containers.forEach(container => {
                        if (container && container.firstElementChild) {
                            const intervalId = setInterval(() => {
                            if (!container || !container.firstElementChild) {
                                clearInterval(intervalId);
                                return;
                            }
                            const firstChild = container.firstElementChild;
                            container.appendChild(firstChild);
                            }, 300);
                        }
                        });
                    }
                    // AI总结相关功能
                    const aiZongjie = document.querySelector('.ai_zongjie');
                    const aiDoubao = document.querySelector('.ai_doubao');
                    const closeButton = document.querySelector('.ai_doubao1_R_img');
                    if (aiZongjie && aiDoubao && closeButton) {
                        aiZongjie.addEventListener('click', () => {
                        aiDoubao.style.display = 'block';
                        });
                        closeButton.addEventListener('click', () => {
                        aiDoubao.style.display = 'none';
                        });
                    }
                    // 文字动画效果
                    const textElements = document.querySelectorAll('.ai_doubao2_3s.ai_doubao2_3s_L p') || [];
                    if (textElements.length > 0) {
                        textElements.forEach(textElement => {
                        if (!textElement) return;
                        const originalHTML = textElement.innerHTML;
                        const tempDiv = document.createElement('div');
                        tempDiv.innerHTML = originalHTML;
                        const fragments = [];
                        Array.from(tempDiv.childNodes).forEach(node => {
                            if (!node) return;
                            if (node.nodeType === Node.TEXT_NODE) {
                            fragments.push(...(node.textContent || '').split(''));
                            } else {
                            fragments.push(node.outerHTML);
                            }
                        });
                        if (fragments.length === 0) return;
                        textElement.innerHTML = '';
                        let index = 0;
                        const interval = setInterval(() => {
                            if (!textElement || index >= fragments.length) {
                            clearInterval(interval);
                            return;
                            }
                            const fragment = fragments[index];
                            if (fragment) {
                            textElement.innerHTML += fragment;
                            }
                            index++;
                        }, 100);
                        });
                    }
                    // 页面滚动监听相关
                    const divai_zongjie1 = document.getElementById('ai_zongjie1');
                    const divai_zongjie2 = document.getElementById('ai_zongjie2');
                    const divai_zongjie3 = document.getElementById('ai_zongjie3');
                    if (divai_zongjie2) {
                        const observer = new IntersectionObserver((entries) => {
                        entries.forEach(entry => {
                            if (!entry.isIntersecting && divai_zongjie1) {
                            try {
                                divai_zongjie1.style.display = 'flex';
                                requestAnimationFrame(() => {
                                if (divai_zongjie1) {
                                    divai_zongjie1.classList.add('visible');
                                }
                                });
                            } catch (e) {
                                console.log('元素操作失败');
                            }
                            } else if (divai_zongjie1) {
                            try {
                                divai_zongjie1.classList.remove('visible');
                                divai_zongjie1.addEventListener('transitionend', () => {
                                if (divai_zongjie1 && !divai_zongjie1.classList.contains('visible')) {
                                    divai_zongjie1.style.display = 'none';
                                }
                                }, { once: true });
                                if (divai_zongjie3 && divai_zongjie3.style) {
                                divai_zongjie3.style.display = 'none';
                                }
                            } catch (e) {
                                console.log('元素操作失败');
                            }
                            }
                        });
                        }, {
                        threshold: 0,
                        rootMargin: '-90px 0px 0px 0px'
                        });
                        try {
                        observer.observe(divai_zongjie2);
                        } catch (e) {
                        console.log('观察器初始化失败');
                        }
                        // 滚动事件处理
                        window.addEventListener('scroll', () => {
                        const scrollY = window.scrollY || window.pageYOffset;
                        if (divai_zongjie2) {
                            try {
                            divai_zongjie2.style.display = scrollY > 1000 ? 'none' : 'block';
                            } catch (e) {
                            console.log('滚动处理失败');
                            }
                        }
                        });
                    }

            </script>
            
        <div class="wzrFour">
            <div class="wzrfour-title">
                <div>最新下载</div>
                <a target="_blank"  href="/xiazai">更多>
                </a>
            </div>
                            <div class="swiper-container  sjyx_banSwiperwz">
                    <ul class="swiper-wrapper">
                                                    <li class="swiper-slide">
                                <a target="_blank"  href="/xiazai/code/8587" target="_blank" title="新鲜有机肉类宣传网站模板">
                                    <img src="https://img.php.cn/upload/webcode/000/000/005/173613957275112.jpg?x-oss-process=image/resize,m_fill,h_220,w_335" onerror="this.onerror='';this.src='/static/images/moren/morentu.png'" alt="新鲜有机肉类宣传网站模板">
                                </a>
                            </li>
                                                    <li class="swiper-slide">
                                <a target="_blank"  href="/xiazai/code/8590" target="_blank" title="驾照考试驾校HTML5网站模板">
                                    <img src="https://img.php.cn/upload/webcode/000/000/011/174954240445579.jpg?x-oss-process=image/resize,m_fill,h_220,w_335" onerror="this.onerror='';this.src='/static/images/moren/morentu.png'" alt="驾照考试驾校HTML5网站模板">
                                </a>
                            </li>
                                                    <li class="swiper-slide">
                                <a target="_blank"  href="/xiazai/code/8588" target="_blank" title="HTML5房地产公司宣传网站模板">
                                    <img src="https://img.php.cn/upload/webcode/000/000/018/173613957345012.jpg?x-oss-process=image/resize,m_fill,h_220,w_335" onerror="this.onerror='';this.src='/static/images/moren/morentu.png'" alt="HTML5房地产公司宣传网站模板">
                                </a>
                            </li>
                                                <div class="clear"></div>
                    </ul>
                    <div class="swiper-pagination"></div>
                </div>
                        
            <div class="wzrfourList">
                <div class="wzrfourlTab">
                    <div class="check" data-id="onef">网站特效 <div></div></div>
                    <div class="" data-id="twof">网站源码<div></div></div>
                    <div class="" data-id="threef">网站素材<div></div></div>
                    <div class="" data-id="fourf">前端模板<div></div></div>
                </div>
                <ul class="onef">
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="jQuery点击文字滚动Scrollocue插件" href="/xiazai/js/8073">[文字特效] jQuery点击文字滚动Scrollocue插件</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="CSS3聚光灯下倒影文字特效" href="/xiazai/js/8072">[文字特效] CSS3聚光灯下倒影文字特效</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="jQuery企业留言表单联系代码" href="/xiazai/js/8071">[表单按钮] jQuery企业留言表单联系代码</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="HTML5 MP3音乐盒播放特效" href="/xiazai/js/8070">[播放器特效] HTML5 MP3音乐盒播放特效</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="HTML5炫酷粒子动画导航菜单特效" href="/xiazai/js/8069">[菜单导航] HTML5炫酷粒子动画导航菜单特效</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="jQuery可视化表单拖拽编辑代码" href="/xiazai/js/8068">[表单按钮] jQuery可视化表单拖拽编辑代码</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="VUE.JS仿酷狗音乐播放器代码" href="/xiazai/js/8067">[播放器特效] VUE.JS仿酷狗音乐播放器代码</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  target="_blank"  title="经典html5推箱子小游戏" href="/xiazai/js/8066">[html5特效] 经典html5推箱子小游戏</a>
                            </div>
                        </li>
                                    </ul>
                <ul class="twof" style="display:none">
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11353" title="雅龙智能装备工业设备类WordPress主题1.0" target="_blank">[企业站源码] 雅龙智能装备工业设备类WordPress主题1.0</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11352" title="威发卡自动发卡系统" target="_blank">[电商源码] 威发卡自动发卡系统</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11351" title="卡密分发系统" target="_blank">[电商源码] 卡密分发系统</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11350" title="中华陶瓷网" target="_blank">[电商源码] 中华陶瓷网</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11349" title="简洁粉色食品公司网站" target="_blank">[电商源码] 简洁粉色食品公司网站</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11348" title="极速网店系统" target="_blank">[电商源码] 极速网店系统</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11347" title="淘宝妈妈_淘客推广系统" target="_blank">[电商源码] 淘宝妈妈_淘客推广系统</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/11346" title="积客B2SCMS商城系统" target="_blank">[电商源码] 积客B2SCMS商城系统</a>
                            </div>
                        </li>
                                    </ul>
                <ul class="threef" style="display:none">
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4114"  target="_blank"  title="极简线条香槟庆祝海报矢量模板">[网站素材] 极简线条香槟庆祝海报矢量模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4113"  target="_blank"  title="手绘健身房运动器材矢量素材">[网站素材] 手绘健身房运动器材矢量素材</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4112"  target="_blank"  title="色彩缤纷新鲜水果矢量素材">[网站素材] 色彩缤纷新鲜水果矢量素材</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4111"  target="_blank"  title="复古美式早午餐海报矢量模板">[网站素材] 复古美式早午餐海报矢量模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4110"  target="_blank"  title="卡通圣诞老人驯鹿圣诞矢量素材">[网站素材] 卡通圣诞老人驯鹿圣诞矢量素材</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4109"  target="_blank"  title="圣诞新年快乐主题海报设计源文件下载">[网站素材] 圣诞新年快乐主题海报设计源文件下载</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4108"  target="_blank"  title="手绘肉类海鲜食材合集矢量素材">[网站素材] 手绘肉类海鲜食材合集矢量素材</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/sucai/4107"  target="_blank"  title="国际癫痫日主题宣传海报模板设计下载">[网站素材] 国际癫痫日主题宣传海报模板设计下载</a>
                            </div>
                        </li>
                                    </ul>
                <ul class="fourf" style="display:none">
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8590"  target="_blank" title="驾照考试驾校HTML5网站模板">[前端模板] 驾照考试驾校HTML5网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8589"  target="_blank" title="驾照培训服务机构宣传网站模板">[前端模板] 驾照培训服务机构宣传网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8588"  target="_blank" title="HTML5房地产公司宣传网站模板">[前端模板] HTML5房地产公司宣传网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8587"  target="_blank" title="新鲜有机肉类宣传网站模板">[前端模板] 新鲜有机肉类宣传网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8586"  target="_blank" title="响应式天气预报宣传网站模板">[前端模板] 响应式天气预报宣传网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8585"  target="_blank" title="房屋建筑维修公司网站CSS模板">[前端模板] 房屋建筑维修公司网站CSS模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8584"  target="_blank" title="响应式志愿者服务网站模板">[前端模板] 响应式志愿者服务网站模板</a>
                            </div>
                        </li>
                                            <li>
                            <div class="wzrfourli">
                                <span class="layui-badge-dots wzrflr"></span>
                                <a target="_blank"  href="/xiazai/code/8583"  target="_blank" title="创意T恤打印店网站HTML5模板">[前端模板] 创意T恤打印店网站HTML5模板</a>
                            </div>
                        </li>
                                    </ul>
            </div>
            <script>
                $('.wzrfourlTab>div').click(function(e){
                    $('.wzrfourlTab>div').removeClass('check')
                    $(this).addClass('check')
                    $('.wzrfourList>ul').css('display','none')
                    $('.'+e.currentTarget.dataset.id).show()
                })
            </script>
        </div>
    </div>
</div>
<!--主体 end-->
<!--底部-->
<div class="phpFoot">
    <div class="phpFootIn">
        <div class="phpFootCont">
            <div class="phpFootLeft">
                <dl>
                    <dt>
                        <a target="_blank"  href="/about/us.html" rel="nofollow" target="_blank" title="关于我们" class="cBlack">关于我们</a>
                        <a target="_blank"  href="/about/disclaimer.html" rel="nofollow" target="_blank" title="免责申明" class="cBlack">免责申明</a>
                        <a target="_blank"  href="/about/jbzx.html" rel="nofollow" target="_blank" title="举报中心" class="cBlack">举报中心</a>
                        <a target="_blank"  href="javascript:;" rel="nofollow" onclick="advice_data(99999999,'意见反馈')"   title="意见反馈" class="cBlack">意见反馈</a>
                        <a target="_blank"  href="https://www.php.cn/teacher.html" rel="nofollow"  target="_blank" title="讲师合作" class="cBlack">讲师合作</a>
                        <a target="_blank"  href="https://www.php.cn/blog/detail/20304.html" rel="nofollow" target="_blank" title="广告合作" class="cBlack">广告合作</a>
                        <a target="_blank"  href="/new/"   target="_blank" title="最新文章列表" class="cBlack">最新更新</a>
                                                <div class="clear"></div>
                    </dt>
                    <dd class="cont1">php中文网:公益在线php培训,帮助PHP学习者快速成长!</dd>
                    <dd class="cont2">
                      <span class="ylwTopBox">
                        <a target="_blank"  href="javascript:;"  class="cBlack"><b class="icon1"></b>关注服务号</a>
                        <em style="display:none;" class="ylwTopSub">
                          <p>微信扫码<br/>关注PHP中文网服务号</p>
                          <img src="/static/images/examples/text16.png"/>
                        </em>
                      </span>
                        <span class="ylwTopBox">
                        <a target="_blank"  href="tencent://message/?uin=27220243&Site=www.php.cn&Menu=yes" target="_blank" class="cBlack"><b class="icon2"></b>技术交流群</a>
                        <em style="display:none;" class="ylwTopSub">
                          <p>QQ扫码<br/>加入技术交流群</p>
                          <img src="/static/images/examples/text18.png"/>
                        </em>
                      </span>
                        <div class="clear"></div>
                    </dd>
                </dl>
                
            </div>
            <div class="phpFootRight">
                <div class="phpFootMsg">
                    <span><img src="/static/images/examples/text17.png"/></span>
                    <dl>
                        <dt>PHP中文网订阅号</dt>
                        <dd>每天精选资源文章推送</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
    <div class="phpFootCode">
        <div class="phpFootCodeIn"><p>Copyright 2014-2025 <a target="_blank"  href="https://www.php.cn/" target="_blank">https://www.php.cn/</a> All Rights Reserved | php.cn | <a target="_blank"  href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">湘ICP备2023035733号</a></p><a target="_blank"  href="http://www.beian.gov.cn/portal/index.do" rel="nofollow" target="_blank"><b></b></a></div>
    </div>
</div>
<input type="hidden" id="verifycode" value="/captcha.html">
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c0e685c8743351838d2a7db1c49abd56";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
<script>layui.use(['element', 'carousel'], function () {var element = layui.element;$ = layui.jquery;var carousel = layui.carousel;carousel.render({elem: '#test1', width: '100%', height: '330px', arrow: 'always'});$.getScript('/static/js/jquery.lazyload.min.js', function () {$("img").lazyload({placeholder: "/static/images/load.jpg", effect: "fadeIn", threshold: 200, skip_invisible: false});});});</script>

<span class="layui-hide"><script type="text/javascript" src="https://s4.cnzz.com/z_stat.php?id=1280886301&web_id=1280886301"></script></span>

<script src="/static/js/cdn.js?v=1.0.1"></script>



<!--底部 end-->
<script>
    $(function() {
        //直播倒计时
        $(".liveitem").each(function(){
            timer(this);
        })
        function timer(obj){
            var intDiff = $(obj).data("countdown");
            window.setInterval(function(){
                var day=0,
                    hour=0,
                    minute=0,
                    second=0;//时间默认值
                if(intDiff > 0){
                    day = Math.floor(intDiff / (60 * 60 * 24));
                    hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
                    minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
                    second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
                }else{
                    $(obj).find(".phpZbktBg").remove();
                    return;
                }
                if (hour <= 9) hour = '0' + hour;
                if (minute <= 9) minute = '0' + minute;
                if (second <= 9) second = '0' + second;
                $(obj).find('.day_show').html(day+"");
                $(obj).find('.hour_show').html('<s id="h"></s>'+hour+'');
                $(obj).find('.minute_show').html('<s></s>'+minute+'');
                $(obj).find('.second_show').html('<s></s>'+second+'');
                intDiff--;
            }, 1000);
        }
    });
</script>
<script type="text/javascript" src="/hitsUp?type=article&id=1495325&time=1766364604"></script>
<script src="/static/ueditor/third-party/SyntaxHighlighter/shCore.js?1766364604"></script>
<script>article_status = "969633";</script>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<!-- <script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script> -->
<input type="hidden" id="verifycode" value="/captcha.html">
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/js/common_new.js?2.1" ></script>
<script type="text/javascript" src="/static/js/global.min.js?5.5.33"></script>
<script>var _hmt = _hmt || [];(function(){var hm = document.createElement("script");hm.src="//hm.baidu.com/hm.js?c0e685c8743351838d2a7db1c49abd56";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();(function(){var bp = document.createElement('script');var curProtocol = window.location.protocol.split(':')[0];if(curProtocol === 'https'){bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';}else{bp.src = 'http://push.zhanzhang.baidu.com/push.js';};var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(bp, s);})();</script>
<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script>var topadshow = $.cookie('phpcndatatopadshows');if(!topadshow&&1==2){$('.topimages').show();var topobj = $('.topimages').find('.time');var topobj_day = $('.topimages .time').find('.day');var topobj_hours = $('.topimages .time').find('.hours');var topobj_minutes = $('.topimages .time').find('.minutes');var topobj_second = $('.topimages .time').find('.second');var topday = parseInt(topobj_day.html());var tophours = parseInt(topobj_hours.html());var topminutes = parseInt(topobj_minutes.html());var topsecond = parseInt(topobj_second.html());setInterval(function(){if(topsecond > 0){topsecond = topsecond-1;}else{if(topminutes > 0){topminutes = topminutes-1;topsecond = 59;}else{if(tophours > 0){tophours = tophours-1;topminutes = 59;topsecond = 59;}else{if(topday > 0){topday = topday -1;tophours = 23;topminutes = 59;topsecond = 59;}else{topobj.html("<p><span>活动已结束</span></p>");}}}}topobj_second.html(topsecond);topobj_minutes.html(topminutes);topobj_hours.html(tophours);topobj_day.html(topday);},1000);}$('.topimages .layui-icon-close').click(function(){$.cookie('phpcndatatopadshows',1,{expires:7});$('.topimages').hide();});</script>
<link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all'/>
<script type='text/javascript' src='/static/js/viewer.min.js?1'></script>
<script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script>
<style>
        .content img{max-width:100%;}
        .copy-button {
            padding: 5px 10px;
            background-color: #666;
            border: none;
            color: #FFF;
            font-size: 12px;
            cursor: pointer;
            border-radius: 5px;
            position: relative;
            top: 33px;
    right: 5px;
    z-index: 99;
    float: right;
        }
        .copy-button:hover {
            background-color: #fc3930;
        }
    </style>
<script>
    $(document).ready(function(){
        $('#gongzhonghao').hover(function(){
            $('#gzh').show();
        },function(){
            $('#gzh').hide();
        })
    })
</script>
<script>
    layui.use(['jquery','layer'], function(){
        $('.test-iframe-handle').click(function(){
                layer.open({
                type: 2,
                area: ['1300px', '750px'],
                content: 'https://www.php.cn/help/ask?q=Golang+encoding%2Fxml%E5%BA%93XML%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%96%B9%E6%B3%95',
                fixed: true, // 不固定
                //maxmin: true,
                shadeClose: true,
                title:"智能小助手",
                btnAlign: 'c',
                yes: function(index, layero){
                    // 获取 iframe 的窗口对象
                    var iframeWin =  window[layero.find('iframe')[0]['name']];
                    var elemMark = iframeWin.$('#mark'); // 获得 iframe 中某个输入框元素
                    var value = elemMark.val();
                    if($.trim(value) === '') return elemMark.focus();
                    // 显示获得的值
                    layer.msg('获得 iframe 中的输入框标记值:'+ value);
                }
                });
            })
        var is_login = "0";
        var show = 0;
        var ceng = getCookie('ceng');
        //文章下拉弹出登录
        // if(is_login == 0 && !ceng)
        // {
        //     window.onscroll = function(){
        //         var t = document.documentElement.scrollTop || document.body.scrollTop;
        //         var top_div = document.getElementById( "top_div" );
        //         if( t >= 2500 && show == 0) {
        //             show = 1
        //             setCookie('ceng',1,1);
        //             $(document).trigger("api.loginpopbox");
        //         }
        //     }
        // }
        //未登录复制显示登录按钮
        if(is_login == 0 && false){
            $(".code").hover(function(){
                $(this).find('.contentsignin').show();
            },function(){
                $(this).find('.contentsignin').hide();
            });
            //不给复制
            $('.code').bind("cut copy paste",function(e) {
                e.preventDefault();
            });
            $('.code .contentsignin').click(function(){
                $(document).trigger("api.loginpopbox");
            })
        }else{
            // 获取所有的 <pre> 元素
            var preElements = document.querySelectorAll('pre');
            preElements.forEach(function(preElement) {
                // 创建复制按钮
                var copyButton = document.createElement('button');
                copyButton.className = 'copy-button';
                copyButton.textContent = '复制';
                // 添加点击事件处理程序
                copyButton.addEventListener('click', function() {
                    // 获取当前按钮所属的 <pre> 元素中的文本内容
                    var textContent = preElement.textContent.trim();
                    // 创建一个临时 textarea 元素并设置其值为 <pre> 中的文本内容
                    var tempTextarea = document.createElement('textarea');
                    tempTextarea.value = textContent;
                    // 将临时 textarea 添加到文档中
                    document.body.appendChild(tempTextarea);
                    // 选中临时 textarea 中的文本内容并执行复制操作
                    tempTextarea.select();
                    document.execCommand('copy');
                    // 移除临时 textarea 元素
                    document.body.removeChild(tempTextarea);
                    // 更新按钮文本为 "已复制"
                    this.textContent = '已复制';
                });

                // 创建AI写代码按钮
                var aiButton = document.createElement('button');
                aiButton.className = 'copy-button';
                aiButton.textContent = 'AI写代码';
                aiButton.style.marginLeft = '5px';
                aiButton.style.marginRight = '5px';
                // 添加点击事件处理程序
                aiButton.addEventListener('click', function() {
                // Generate a random number between 0 and 1
                        var randomChance = Math.random();

                    // If the random number is less than 0.5, open the first URL, else open the second
                    if (randomChance < 0.5) {
                        window.open('https://www.doubao.com/chat/coding?channel=php&source=hw_db_php', '_blank');
                    } else {
                        window.open('https://click.aliyun.com/m/1000402709/', '_blank');
                    }
                });

                // 将按钮添加到 <pre> 元素前面
                preElement.parentNode.insertBefore(copyButton, preElement);
                preElement.parentNode.insertBefore(aiButton, preElement);
        });
        }
    })
    function setCookie(name,value,iDay){      //name相当于键,value相当于值,iDay为要设置的过期时间(天)
        var oDate = new Date();
        oDate.setDate(oDate.getDate() + iDay);
        document.cookie = name + '=' + value + ';path=/;domain=.php.cn;expires=' + oDate;
    }
    function getCookie(name) {
        var cookieArr = document.cookie.split(";");
        for(var i = 0; i < cookieArr.length; i++) {
            var cookiePair = cookieArr[i].split("=");
            if(name == cookiePair[0].trim()) {
                return decodeURIComponent(cookiePair[1]);
            }
        }
        return null;
    }

    function aiask(ask){
        layer.open({
            type: 2,
            area: ['1300px', '750px'],
            content: 'https://www.php.cn/help/ask?q='+encodeURIComponent(ask),
            fixed: true, // 不固定
            //maxmin: true,
            shadeClose: true,
            title:"智能小助手",
            btnAlign: 'c',
            yes: function(index, layero){
                // 获取 iframe 的窗口对象
                var iframeWin =  window[layero.find('iframe')[0]['name']];
                var elemMark = iframeWin.$('#mark'); // 获得 iframe 中某个输入框元素
                var value = elemMark.val();
                if($.trim(value) === '') return elemMark.focus();
                // 显示获得的值
                layer.msg('获得 iframe 中的输入框标记值:'+ value);
            }
        });
    }

</script>
<!--底部浮动层-->
<!--
    <div class="phpFudong">
        <div class="phpFudongIn">
            <div class="phpFudongImg"></div>
            <div class="phpFudongXue">登录PHP中文网,和优秀的人一起学习!</div>
            <div class="phpFudongQuan">全站<span>2000+</span>教程免费学</div>
            <div class="phpFudongCode"><a href="javascript:;" id="login" title="微信扫码登录">微信扫码登录</a></div>
            <div class="phpGuanbi" onclick="$('.phpFudong').hide();"></div>
            <div class="clear"></div>
        </div>
    </div>
--><!--底部浮动层 end-->
<!--侧导航-->
<style>
    .layui-fixbar{display: none;}
</style>
<div class="phpSdhBox" style="height:240px !important;">
    <li>
        <div class="phpSdhIn">
            <div class="phpSdhTitle">
                <a href="/k24.html" target="_blank" class="hover" title="PHP学习">
                    <b class="icon1"></b>
                    <p>PHP学习</p>
                </a>
            </div>
        </div>
    </li>
    <li>
        <div class="phpSdhIn">
            <div class="phpSdhTitle">
                <a href="https://www.php.cn/blog/detail/1047189.html" target="_blank">
                    <b class="icon2"></b>
                    <p>技术支持</p>
                </a>
            </div>
        </div>
    </li>
    <li>
        <div class="phpSdhIn">
            <div class="phpSdhTitle">
                <a href="#">
                    <b class="icon6"></b>
                    <p>返回顶部</p>
                </a>
            </div>
        </div>
    </li>
</div>
<!--侧导航 end-->
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://tongji.php.cn/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '11']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

<script>
    setCookie('is_article', 1, 1);
</script>
</body>
</html>