Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如果尝试从通道调用字符串数据,则会出现错误_C_User Interface_Go_Github_Window - Fatal编程技术网

C 如果尝试从通道调用字符串数据,则会出现错误

C 如果尝试从通道调用字符串数据,则会出现错误,c,user-interface,go,github,window,C,User Interface,Go,Github,Window,我正在尝试编写一个程序,该程序应该从RSS链接中获取一组数据(这部分工作正常),然后将其存储在GUI窗口中。但是,我的代码显示GUI窗口约一毫秒,然后出现以下错误: panic: runtime error: index out of range goroutine 1 [running, locked to thread]: main.(*modelHandler).CellValue(0xc0000e81b0, 0xc0000760e0, 0x0, 0x0, 0x8, 0xc00004c3

我正在尝试编写一个程序,该程序应该从RSS链接中获取一组数据(这部分工作正常),然后将其存储在GUI窗口中。但是,我的代码显示GUI窗口约一毫秒,然后出现以下错误:

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
这是我迄今为止编写的代码:

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)
}

但是,我不知道如何将其转换回常规字符串。

您正在调用全新频道结构上的.Items[row].Title(和其他)-该代码中的某些内容不正确,因为此处的任何行值都会导致类似的错误。。。
(由于项目未初始化)

我决定调用通道{}.Items,而不是调用new_rss.Items。不幸的是,我仍然收到一个错误。您建议我如何初始化Items数组?我不知道该怎么做。你需要以这样或那样的方式将解析后的“rss”对象传递到你的UI代码中。最简单(但不干净)的方法是将该变量设为全局变量,然后使用“rss.Items”而不是“new_rss.Items”。
new_rss.Items[row].Location