An IRC-Bot in Go

I was looking around on the web confused as always and i found some interesting projects on Github that were built using a programming language i had never heard of before “Golang”. I immediately started looking up what Golang was and noticed that its a C/C++ inspired language used for a good range of different applications, its a compiled language which is attempting to bring the best of dynamic typed languages like Javascript and compiled languages such as Java. I got a book and started to read more about Go. I decided to switch it up and start writing in English to reach a bigger audience.

So i started with step by step walking through the book called “The Little Go Book” its a good book explaining most parts very well. So i followed the book from start to finish as well as taking noted and trying to write small examples and extending code provided in the book. It was going well and everything was working as intended.

After reading the book i was struck with how great it felt coding in Go. Before i was a bit skeptic to getting my hands dirty with a compiled language. But when reading a little bit more and trying for myself i noticed that the compile time is almost non existent, its so fast and easy to get started with.

I moved on creating a real project not just test code, i decided after thinking a while and finding the net package to create my own bot that connects to an IRC chat and “remembers” users as well as their messages.

The development of the bot was really smooth and i almost never encountered any problems with using Go. Dependency management is really easy using go get and i just overall got encouraged to just write code.

So i wanted this bot to actually do something so before i started coding i started writing up some features i wanted the bot to have:

  • Logging users & their messages to a sqlite database.
  • Able to print out .txt files of user loggs.
  • Listen to user commands both from the chat & the terminal.
  • Able to evaluate statements from the chat Example: !eval 5 + 5 would return 10

There were two things i found difficult and it was to create the eval feature. Implementing a reader that reads identifiers from a string was not that supported and this is what i came up with:

ast.Inspect(tr, func(n ast.Node) bool {
var s string
switch x := n.(type) {
case *ast.BasicLit:
s = x.Value
Evalvalues = append(Evalvalues, s)
case *ast.Ident:
s = x.Name
Evalvalues = append(Evalvalues, s)
case *ast.BinaryExpr:
s = x.Op.String()
operands = append(operands, s)
}
if s != “” {
fmt.Print(“Pos: “, n.Pos(), ” Value: “, s)
}
return true
})

This piece of code is using the ast package and it is inspecting nodes parsed from a string looking at all identifiers and literals. It is placing all the values found (Numbers, Strings, etc) into one interface slice and the operands (+, -, * etc) is placed into a separate interface slice. The empty interface is something i had a hard time to understand at first in Go. There is no such thing as optional parameters in Go so you will have to use interfaces. So when coding in Go i noticed two parts you will have to read a few extra parts is about interfaces and type assertion since you will need it.

I also created a feature of blocking users, blocked users cannot use the commands for the bot in the chat. Currently the featured chat commands is:

  • !help – Displays help message
  • !status – Displays uptime, users & messages tracked
  • !eval – Evaluates a statement

Another thing i like about Go is concurrency, its a feature you can compare to threads where you have multiple processes running at the time. In my bot application i was determined that i was going to use Golangs concurrency features. There is three different processes running in my bot application, two processes for each control panel (one for the terminal and one for the chat) and one process for the connection and listener. Go uses “channels” to communicate between processes and in my application i am using one channel to send strings from the chat reader to the chat control panel.

My weeks of using Go has been great and i encourage people who want to try new things to try learn Golang, its easy to learn and very fun and rewarding as well.

My next projects is a Weather API which i will write more about here.

Untill next time!

Leave a Reply

Your email address will not be published. Required fields are marked *