dow5001 2019-03-22 21:58
浏览 176
已采纳

Go需要逗号,当放在那里时,会引发其他不相关的错误

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条回答 默认 最新

  • dqspy04266 2019-03-22 22:04
    关注

    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.")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?