FAQ
https://play.golang.org/p/nrW6K4cZGl

package main

import (
"encoding/xml"
"fmt"
)

func main() {
type agent_job_configs struct{
ID string `xml:"id"`
Schedule string `xml:"schedule,attr"`
Job_type []string `xml:"jobs>job>job_type"`
Start_date []string `xml:"jobs>job>start_date"`
Pattern []string `xml:"jobs>job>pattern"`
Daily_type []string `xml:"jobs>job>daily_type"`
Daily_recur []string `xml:"jobs>job>daily_recur"`
Parameters []string `xml:"jobs>job>parameters"`
Queue []string `xml:"jobs>job>queue"`
WEEk_days []string `xml:"jobs>job>week_days"`
}
v := agent_job_configs {}

data := `
<cm>
<id>AGENT_JOB_CONFIGS</id>
<jobs>
      <job>
<job_type>ASSET_INFO</job_type>
<start_date>1/2/2016 16:25:00</start_date>
<pattern>1</pattern>
<daily_type>1</daily_type>
<daily_recur>1</daily_recur>
<parameters></parameters>
<queue>1</queue>
      </job>
<job>
<job_type>ASSET_INFO</job_type>
<start_date>1/2/2016 16:25:00</start_date>
<pattern>2</pattern>
<week_days>0010100</week_days>
<parameters></parameters>
<queue>1</queue>
</job>
</jobs>
</cm>
`
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
for i,_ :=range v.Job_type {
fmt.Println("Job_type: ",v.Job_type[i])
fmt.Println("Start_date: ",v.Start_date[i])
fmt.Println("Pattern: ",v.Pattern[i])
if v.Pattern[i]=="1"{
fmt.Println("I value is : ",i)
fmt.Println("Daily_type: ",v.Daily_type[i])
fmt.Println("Daily_recur: ",v.Daily_recur[i])
}else if v.Pattern[i]=="2"{
fmt.Println("I value is : ",i)
fmt.Println("Queue :",v.Queue[i])
fmt.Println("week_days :",v.WEEk_days[i])
}
}
}

I don"t know why its give runtime index out of range error
Please help me out

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Jan 2, 2016 at 11:43 am
    WEEk_days is only appended to if pattern == 2, so there will be no
    WEEk_days entry at index 2.
    On Saturday, 2 January 2016 12:35:28 UTC+1, kumargv wrote:


    https://play.golang.org/p/nrW6K4cZGl

    package main

    import (
    "encoding/xml"
    "fmt"
    )

    func main() {
    type agent_job_configs struct{
    ID string `xml:"id"`
    Schedule string `xml:"schedule,attr"`
    Job_type []string `xml:"jobs>job>job_type"`
    Start_date []string `xml:"jobs>job>start_date"`
    Pattern []string `xml:"jobs>job>pattern"`
    Daily_type []string `xml:"jobs>job>daily_type"`
    Daily_recur []string `xml:"jobs>job>daily_recur"`
    Parameters []string `xml:"jobs>job>parameters"`
    Queue []string `xml:"jobs>job>queue"`
    WEEk_days []string `xml:"jobs>job>week_days"`
    }
    v := agent_job_configs {}

    data := `
    <cm>
    <id>AGENT_JOB_CONFIGS</id>
    <jobs>
    <job>
    <job_type>ASSET_INFO</job_type>
    <start_date>1/2/2016 16:25:00</start_date>
    <pattern>1</pattern>
    <daily_type>1</daily_type>
    <daily_recur>1</daily_recur>
    <parameters></parameters>
    <queue>1</queue>
    </job>
    <job>
    <job_type>ASSET_INFO</job_type>
    <start_date>1/2/2016 16:25:00</start_date>
    <pattern>2</pattern>
    <week_days>0010100</week_days>
    <parameters></parameters>
    <queue>1</queue>
    </job>
    </jobs>
    </cm>
    `
    err := xml.Unmarshal([]byte(data), &v)
    if err != nil {
    fmt.Printf("error: %v", err)
    return
    }
    for i,_ :=range v.Job_type {
    fmt.Println("Job_type: ",v.Job_type[i])
    fmt.Println("Start_date: ",v.Start_date[i])
    fmt.Println("Pattern: ",v.Pattern[i])
    if v.Pattern[i]=="1"{
    fmt.Println("I value is : ",i)
    fmt.Println("Daily_type: ",v.Daily_type[i])
    fmt.Println("Daily_recur: ",v.Daily_recur[i])
    }else if v.Pattern[i]=="2"{
    fmt.Println("I value is : ",i)
    fmt.Println("Queue :",v.Queue[i])
    fmt.Println("week_days :",v.WEEk_days[i])
    }
    }
    }

    I don"t know why its give runtime index out of range error
    Please help me out
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 2, '16 at 11:35a
activeJan 2, '16 at 11:43a
posts2
users2
websitegolang.org

2 users in discussion

Dave Cheney: 1 post Kumargv: 1 post

People

Translate

site design / logo © 2022 Grokbase