0

I have the eclipse software ready to go.

What I am looking for is to debug output the return data for calling search.RetrieveFeeds().

So, in essence, I'd be able to see an array of Feed structs so i know it processed the json data file

When I execute I get this error:

main\main.go:10:38: multiple-value search.RetrieveFeeds() in single-value context

I looked here: Multiple values in single-value context but it was a bit over my head.

Here is what I have so far as my architecture:

Data Json File Path

src/data/data.json

Data for Json File

[
    {
        "site": "npr",
        "link": "http://www.npr.org/rss/rss.php?id=1001",
        "type": "rss"
    },
    {
        "site": "cnn",
        "link": "http://rss.cnn.com/rss/cnn_world.rss",
        "type": "rss"
    },
    {
        "site": "foxnews",
        "link": "http://feeds.foxnews.com/foxnews/world?format=xml",
        "type": "rss"
    },
    {
        "site": "nbcnews",
        "link": "http://feeds.nbcnews.com/feeds/topstories",
        "type": "rss"
    }
]

Main Method File Path

src/main/main.go

Code for Main Method

package main

import (
    "fmt"
    "search"
)

func main() {
     fmt.Println("HellAo")     
     var feeds = search.RetrieveFeeds()
     fmt.Printf("%v",feeds)
}

Search File Path

src/search/feed.go

Search File Code

package search

import (
  "encoding/json"
  "os"
)

const dataFile = "data/data.json"

type Feed struct {
    Name string `json:"site"`
    URI string `json:"link"`
    Type string `json:"type"`
}

func RetrieveFeeds() ([]*Feed, error){
    file, err := os.Open(dataFile)
    if err != nil {
        return nil, err
    }

    defer file.Close()

    var feeds []*Feed
    err = json.NewDecoder(file).Decode(&feeds)

    return feeds, err
}

UPDATE

I changed the data json path to:

const dataFile = "src/data/data.json"

And now the debug dump says:

<nil>

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
somejkuser
  • 8,856
  • 20
  • 64
  • 130
  • 4
    `var feeds, _ = search.RetrieveFeeds()`. Even better `var feeds, err = search.RetrieveFeeds(); if err != nil { ...`. If a function in Go can return more than one value you need to have the same number of variables, or `_`s, on the LHS to assign all the values not just the first one. – mkopriva May 31 '18 at 18:05
  • 3
    It might serve you well to walk through the [Tour of Go](https://tour.golang.org/). Understanding multiple returns values is pretty fundamental to using the language. – JimB May 31 '18 at 18:07
  • @JimB Im working through the ebook Go in Action. I'll look at that next. It seems to debug output but the contents are empty. Maybe my data file declaration isnt right? – somejkuser May 31 '18 at 18:09
  • @jkushner your RetrieveFeeds code seems correct to me, make sure you check the error returned from it before you attempt to print the contents of the feeds. – mkopriva May 31 '18 at 18:11
  • Here it says: open data.json: The system cannot find the file specified. – somejkuser May 31 '18 at 18:12
  • 1
    @jkushner That's pretty clear then. Make sure the path to the file is correct, and if you use relative paths make sure the program is executed from that directory from which the relative path correctly loactes the file you want to open. – mkopriva May 31 '18 at 18:13
  • Got it thanks a lot! – somejkuser May 31 '18 at 18:15
  • 1
    You do not need a debugger when your program is not running. You must fix the compiler errors first. – Volker May 31 '18 at 19:03

0 Answers0