DEV Community

Daniel Brum
Daniel Brum

Posted on • Updated on

Creating a Reddit repost bot with Go

I created a Reddit repost bot as my first Go program, it was an interesting experience and I'm sharing this here to help people interested in doing something like that. I'm also open to feedback since I'm new to Go.

This post is divided into sections, I guess that the development process is explained better this way. My intention here is to show my experience with the process, the code is at the end.

Why?

At the time I decided to code this bot, I was already looking into Go, I just needed an idea. I use Twitter and Reddit a lot, so I came up with the idea to develop a Reddit repost bot.

Coding a bot like that would probably be enough for me to learn what I wanted. I was aiming at some Go basics like functions and structs, and also I was looking forward to learning about HTTP servers and JSON in Go.

How?

The start was a little messy for me. I decided to make a request and get the data I wanted from the subreddit response body. I searched for a little bit and found GoQuery, it's basically jQuery but written in Go.

After some time, I had the data, and then it was time for me to make my Post struct and then send it to the Twitter API and tweet the data. I started having some trouble here, I wasn't able to get the data in a format that I like, so I ended up looking for an alternative.

I found out that Reddit has everything in JSON, this would make my work a lot easier. I could've looked for it when I started to code, but living and learning, right?

I threw away the GoQuery part and refactored the code to contemplate JSON. It wasn't a lot to change, so I didn't have trouble with it.

After that, I created a function that would return a random post and then send it to the Twitter API.

As far as I can remember, this was the development process.

Difficulties I had

When we are learning something new, difficulties are normal. They are part of the process and help you to understand something and do better.

I had some challenges with the language itself and with libraries that I've used, I'm going to tell you about them now.

The first problem came up with GoQuery, I did everything I needed to get the data, but it ended up not being the way I thought, so I researched a little bit and started to use JSON as I've said before.

Another thing was that I took some time to understand Go. How the language works and the language philosophy took a while to stick. After I realized that the language doesn't come up with a lot of built-in stuff and I had to build them myself, things got easier.

I also struggled with the JSON itself when I created the structs. I thought that I had to declare all the keys from the JSON body on the struct, that would be a great amount of work. I researched a little bit and figured out that I only need to add what I want on the struct. I guess that this one was the fastest to learn and fix.

The Go experience

I work with TypeScript, so coding in Go was very different. This is not bad in any way, it's actually pretty cool to see the differences between the tools we use.

Before I started to code the bot, I've had already done the Go tour, so I knew a little bit about the language basics. While coding, I basically used the documentation and searched what I needed, and tried to implement it. I also joined the Go slack to ask for help, the people there were very nice to me.

I struggled a little bit to understand how to do some things because I was used to code in languages that have a lot of stuff out of the box. After I understood it, things got easier.

Go is a very good language, it's not hard to learn the basics, and you start seeing results fast. This is probably due to the "keep it simple" philosophy behind the language. The learning experience isn't hard, the documentation is good and the examples are clear.

That was my experience with Go, I really liked the language and I'll continue to study it. At the moment I'm reading the Go Programming Language book to understand more about the language itself.

Thank you for reading, I hope you liked it!

Here is the code:

GitHub logo banidrum / golang-repost-bot

A bot written in Go that reposts stuff from subreddits on Twitter.

Top comments (0)