Search code examples
goreddit

Go requires comma, when put there, other unrelated errors are thrown


I'm trying to create a reddit bot in Golang using this library, and Golang is asking for a comma, however, when I put it there, Go throws other errors.

Here's my main.go:

package main

import (
  "github.com/turnage/graw/reddit"
)

func main() {
  cfg := BotConfig{
    Agent: "graw:doc_demo_bot:0.3.1 by /u/yourusername",
    // Your registered app info from following:
    // https://github.com/reddit/reddit/wiki/OAuth2
    App: App{
      ID:     "sdf09ofnsdf",
      Secret: "skldjnfksjdnf",
      Username: "yourbotusername",
      Password: "yourbotspassword",
    }
  }
  bot, _ := NewBot(cfg)
  bot.SendMessage("roxven", "Thanks for making this Reddit API!", "It's ok.")
}

Here's the output with the code above (no comma at 17:7):

# command-line-arguments
./main.go:17:6: syntax error: unexpected newline, expecting comma or }

Here's the output when I put the comma there:

# command-line-arguments
./main.go:4:3: imported and not used: "github.com/turnage/graw/reddit"
./main.go:8:10: undefined: BotConfig
./main.go:19:13: undefined: NewBot

I've also tried putting a comma after line 16 (so that there are two) and I get this error:

# command-line-arguments
./main.go:16:36: syntax error: unexpected comma, expecting expression
./main.go:17:6: syntax error: unexpected newline, expecting comma or }

I'm not sure what I'm doing wrong.


Solution

  • Your errors (after fixing the syntax issue by adding the comma) are all related to each other. As written, you aren't using the package you've imported. Use reddit.BotConfig, reddit.App, and reddit.NewBot to use the structs and functions from that package. Importing in Go doesn't bring things into a global top-level namespace.

    func main() {
        cfg := reddit.BotConfig{
            Agent: "graw:doc_demo_bot:0.3.1 by /u/yourusername",
            // Your registered app info from following:
            // https://github.com/reddit/reddit/wiki/OAuth2
            App: reddit.App{
                ID:       "sdf09ofnsdf",
                Secret:   "skldjnfksjdnf",
                Username: "yourbotusername",
                Password: "yourbotspassword",
            },
        }
        bot, _ := reddit.NewBot(cfg)
        bot.SendMessage("roxven", "Thanks for making this Reddit API!", "It's ok.")
    }