dongshi9526 2018-12-14 23:00
浏览 271

如果我尝试从Channel调用字符串数据,则会出现错误结果

I am trying to write a program that is supposed to fetch a bunch of data from an RSS link (that part works), and then store it in a GUI window. However, my code shows the GUI window for about a millisecond, then gives me this error:

panic: runtime error: index out of range

goroutine 1 [running, locked to thread]:
main.(*modelHandler).CellValue(0xc0000e81b0, 0xc0000760e0, 0x0, 0x0, 0x8, 0xc00004c380)
    C:/Users/Owner/go/src/FinalProject/GUI-Based System.go:71 +0x517
github.com/andlabs/ui.pkguiDoTableModelCellValue(0x9ba820, 0x17ba1b0, 0x0, 0x4047a0)
    C:/Users/Owner/go/src/github.com/andlabs/ui/tablemodel.go:171 +0xc3
github.com/andlabs/ui._cgoexpwrap_7d4d3b498194_pkguiDoTableModelCellValue(0x9ba820, 0x17ba1b0, 0x0, 0x0)
    _cgo_gotypes.go:4087 +0x7f
github.com/andlabs/ui._Cfunc_uiControlShow(0x3977350)
    _cgo_gotypes.go:1624 +0x48
github.com/andlabs/ui.(*ControlBase).Show.func1(0x3977350)
    C:/Users/Owner/go/src/github.com/andlabs/ui/control.go:108 +0x5d
github.com/andlabs/ui.(*ControlBase).Show(0xc000394380)
    C:/Users/Owner/go/src/github.com/andlabs/ui/control.go:108 +0x36
main.setupUI()
    C:/Users/Owner/go/src/FinalProject/GUI-Based System.go:131 +0x2b2
github.com/andlabs/ui.pkguiDoQueueMain(0x0)
    C:/Users/Owner/go/src/github.com/andlabs/ui/main.go:103 +0xb0
github.com/andlabs/ui._cgoexpwrap_7d4d3b498194_pkguiDoQueueMain(0x0)
    _cgo_gotypes.go:3967 +0x32
github.com/andlabs/ui._Cfunc_uiMain()
    _cgo_gotypes.go:2519 +0x48
github.com/andlabs/ui.Main(0x8037a8, 0x77e420, 0xc000048920)
    C:/Users/Owner/go/src/github.com/andlabs/ui/main.go:41 +0x104
main.main()
    C:/Users/Owner/go/src/FinalProject/GUI-Based System.go:151 +0x1b7

Process finished with exit code 2

This is the code I have written so far:

package main

import (
    "encoding/xml"
    "github.com/andlabs/ui"
    _ "github.com/andlabs/ui/winmanifest"
    "log"
    "net/http"
    "regexp"
    "strings"
)

type JobInfo struct{
    Title string `xml:"title"`
    Location string `xml:"location"`
    PostDate string `xml:"pubDate"`
    Description string `xml:"description"`
}

type Channel struct{
    Title string `xml:"title"`
    Link string `xml:"link"`
    Desc string `xml:"description"`
    Items []JobInfo `xml:"item"`
}

type Rss struct {
    Channel []Channel `xml:"channel"`
}

type modelHandler struct{
    row9Text string
    yellowRow int
    checkStates [10000]int
}

func replace_weird_characters(s string) string{
    reg, err := regexp.Compile("&nbsp;|<[a-zA-Z /]+>")
    if err != nil{
        log.Fatal(err)
    }
    new_string := string(reg.ReplaceAllString(s, " "))
    return new_string
}

func newModelHandler() *modelHandler {
    m := new(modelHandler)
    m.row9Text = "You can edit this one"
    m.yellowRow = -1
    return m
}

func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue {
    return []ui.TableValue{
        ui.TableString(""),
        ui.TableString(""),
        ui.TableString(""),
        ui.TableString(""),
        ui.TableString(""),
        ui.TableColor{},
        ui.TableColor{},
    }
}

func (mh *modelHandler) NumRows(m *ui.TableModel) int {
    return 10000
}

func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue {
    new_rss := Channel{}
    if column == 0{
        return ui.TableString(new_rss.Items[row].Title[:strings.LastIndex(new_rss.Items[row].Title," at ")])
    }
    if column == 1{
        return ui.TableString(new_rss.Items[row].Location)
    }
    if column == 2 {
        company := new_rss.Items[row].Title[strings.LastIndex(new_rss.Items[row].Title, " at "):strings.LastIndex(new_rss.Items[row].Title, " (")]
        real_company_name := strings.Replace(company, " at ", "", -1)
        return ui.TableString(real_company_name)
    }
    if column == 3 {
        postdate := strings.Replace(new_rss.Items[row].PostDate, "Z", "", -1)
        return ui.TableString(postdate)
    }
    if column == 4 {
        real_description := replace_weird_characters(new_rss.Items[row].Description)
        return ui.TableString(real_description)
    }
    panic("unreachable")
}

func (mh *modelHandler) SetCellValue(m *ui.TableModel, row, column int, value ui.TableValue) {
    if row == 9 && column == 2 {
        mh.row9Text = string(value.(ui.TableString))
    }
}

func setupUI()  {
    mainwin := ui.NewWindow("something", 640, 480, true)
    mainwin.OnClosing(func(*ui.Window) bool {
        ui.Quit()
        return true
    })
    ui.OnShouldQuit(func() bool {
        mainwin.Destroy()
        return true
    })

    mh := newModelHandler()
    model := ui.NewTableModel(mh)
    table := ui.NewTable(&ui.TableParams{
        Model: model,
    })

    mainwin.SetChild(table)
    mainwin.SetMargined(true)

    table.AppendTextColumn("Title",
        0, ui.TableModelColumnNeverEditable, nil)

    table.AppendTextColumn("Location",
        1, ui.TableModelColumnNeverEditable, nil)
    table.AppendTextColumn("Company",
        2, ui.TableModelColumnNeverEditable, nil)

    table.AppendTextColumn("Post Date",
        3, ui.TableModelColumnNeverEditable, nil)
    table.AppendTextColumn("Description",
        4, ui.TableModelColumnNeverEditable, nil)

    mainwin.Show()
}

func main() {
    resp, err := http.Get("https://stackoverflow.com/jobs/feed?l=Bridgewater%2c+MA%2c+USA&u=Miles&d=100")
    if err != nil{
        log.Fatal(err)
        return
    }
    defer resp.Body.Close()

    rss := Rss{}

    decoder := xml.NewDecoder(resp.Body)
    err = decoder.Decode(&rss)
    if err != nil{
        log.Fatal(err)
        return
    }

    ui.Main(setupUI)
}

I have determined that it results in an error when I'm trying to do things like

new_rss.Items[row].Location

However, I'm not sure how to turn it back into a regular string.

  • 写回答

1条回答 默认 最新

  • dongyin5516 2018-12-15 00:26
    关注

    You are calling .Items[row].Title (and others) on a brand new Channel struct - something in that code is incorrect, as any row value here will cause an error like that... (As the Items are uninitialised)

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助