Program 1: Build a simple text bot

Wouldn’t it be awesome to build a chatbot like those hackers we read about in the news and see on movies? This is the beginning of that journey. If you’ve read the previous post, you might remember it recommended downloading Golang, and, getting an IDE or text editor. I recommend trying out Goland. If you’ve not installed these – please do so now. There are a number of ‘guides’ for getting started in golang on the internet – These can be a great cross reference point – or – even better – a Hello World tutorial. (For some reason developers like to try out languages by writing a program that says ‘Hello World’). Step through the tutorial – it won’t teach a lot – but it gives a basic, very basic, program – to make sure you’re all setup correctly.

Step 1: Hello World

I recommend following: Hello World | Learn Go Programming (

Step 2: Explanation

Golang has a few fun ‘pieces’ to it. Here is the example code from the link above:

package main

import "fmt"

func main() {
   fmt.Println("Hello, World!")

In the above – it has a couple of specific items. package main identifies what this piece of the program is. In Golang, main is the primary piece – the place were everything starts. When golang compiles (takes the code and turns it into machine code) it looks for main as the start point.

The next line, import “fmt” indicates that golang needs to go grab someone else’s code. A thing called “fmt” which is a format library for strings. If you’ll remember from the last post, strings are a type of variable. Now, you might have guessed this from reading it, but, the fmt package has some prebuilt items that make golang quite powerful.

The next line func main() { is the beginning of a golang function. A function being a collection of commands that run when that function is called. You can see that it has an opening curly brace – and – that there is a closing curly brace further down the screen. These braces indicate where the function starts and stops. In golang – the word ‘main’ continues to be important. The main package that we noted above? Well, the compiler looks for a main func inside the main package. It uses this when the program starts.

The final piece, is the actual command inside the curly braces. This is what tells the computer something needs to be printed to the screen. It uses the fmt library to print, because printing is such a common operation in programming, it’s best that everyone not reinvent the wheel printing out things.

Step 3: Echobot

Now that we’ve got a working helloworld program, let’s see if we can turn it into an echobot. In certain languages, echo is the word used to describe writing a statement to a terminal window. It’s not universal, but it seems fitting for a bot name. Now, to start of coding this bot, let’s think in terms of flow (and remember, this is just a text bot – nothing too fancy.

  1. launch program
  2. send a friendly greeting
  3. send a simple instruction
  4. Have the user put something in
  5. echo what the user put in
  6. repeat 4 and 5 until the user says ‘bye’

Well, in looking at this – the first 3 steps we know how to do. The 4th will require knowing how to get something from a user. 6 is the most complicated. We need to keep getting things, and, we need to check if the user is done.

package main

import (

// main function
func main() {
	var input string

	fmt.Println("Hello, World!")
	fmt.Println("I am Echo! Type something, and pressing enter!")

	// Stop when we see "bye"
	for input != "bye" {

		fmt.Print("You: ")
		scanner := bufio.NewScanner(os.Stdin)
		for scanner.Scan() {
			input = scanner.Text()

		fmt.Print("Echo: " + input + "\n")

The example above introduces the golang for loop. We can cover that more in a later lesson. For now – just understand, for will keep running until the condition to the right is satisfied. We’ve also introduced a bufio.NewScanner(os.Stdin). You don’t have to worry too much about what that means right now, just that the whole string, means “read what the user typed, and then store it in scanner”. The next for scanner.Scan() Is telling the program to actually wait until the user completes what they are typing. Computers like to be fast when they can. They aren’t doing anything when waiting for a user to type. In golang, it is more obvious execution is being blocked by …*cough* the user *cough*.

Running the above program, and typing in test<enter>cool<enter>bye<enter> will have something like the following output:

example program output screenshot.

There are lots of things to explore in building a chatbot – and in building more advanced code. We’ve not even begun to scratch the surface yet.

Published by

Kyle Wiering

I am Christian, a Software Engineer, and a Yooper living in Austin, TX - U.S.

One thought on “Program 1: Build a simple text bot”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.