-5

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.

2 Answers2

2

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.")
}
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
1

You are fine to put a , after

App: App{
  ID:     "sdf09ofnsdf",
  Secret: "skldjnfksjdnf",
  Username: "yourbotusername",
  Password: "yourbotspassword",
}, //like this

The other errors are actually the errors that you will need to fix. Golang is strict and will not allow unused imports or unused variables. Also, you have to import packages that contain the definitions of the structs that you use - BotConfig and NewBot.

You can name your imports so you can refer to your imports without having to do reddit.BotConfig. For ex

import r "github.com/turnage/graw/reddit"

This will allow you to simply use r.BotConfig for ex. Otherwise for each time you want to use BotConfig you'll have to refer to the package name as reddit.BotConfig

perennial_noob
  • 459
  • 3
  • 14