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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。