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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?