Golang
Golang






Go Programming: Simple Chatbot

Introduction

Chatbots are computer programs that simulate conversations with users. They are widely used in customer support, information retrieval, and entertainment.
This program demonstrates how to create a simple chatbot in Go that responds to user inputs with predefined replies.

Objective

The objective of this project is to build a console-based chatbot that can respond to user queries dynamically using basic input handling and conditional logic.
This project introduces learners to Go’s string manipulation and standard input/output capabilities.

Code


package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	fmt.Println("Welcome to GoBot!")
	fmt.Println("You can ask me simple questions like 'How are you?' or 'What is your name?'. Type 'exit' to quit.")

	// Scanner for reading user input
	reader := bufio.NewReader(os.Stdin)

	for {
		fmt.Print("\nYou: ")
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("Error reading input. Please try again.")
			continue
		}

		// Clean up the input
		input = strings.TrimSpace(strings.ToLower(input))

		// Exit condition
		if input == "exit" {
			fmt.Println("GoBot: Goodbye! Have a great day!")
			break
		}

		// Respond to user input
		switch input {
		case "hello", "hi":
			fmt.Println("GoBot: Hello! How can I assist you today?")
		case "how are you":
			fmt.Println("GoBot: I'm just a bot, but I'm doing great! How about you?")
		case "what is your name":
			fmt.Println("GoBot: My name is GoBot. I'm here to help you!")
		case "what can you do":
			fmt.Println("GoBot: I can chat with you and respond to some basic questions. Try asking 'How are you?' or 'What is your name?'.")
		default:
			fmt.Println("GoBot: I'm sorry, I don't understand that. Can you try asking something else?")
		}
	}
}

            

Explanation

The program structure is as follows:

  • User Input: The program uses bufio.NewReader to read user input from the console. Input is processed using string manipulation functions to handle variations in capitalization and spacing.
  • Chatbot Responses: A switch statement is used to match user input with predefined questions or commands. Responses are provided for specific queries, and a fallback response handles unrecognized inputs.
  • Exit Condition: Typing “exit” ends the chatbot session gracefully, displaying a farewell message.

How to Run the Program

  1. Ensure you have Go installed on your system. You can download it from
    Go’s official website.
  2. Save the code in a file named simple_chatbot.go.
  3. Open a terminal and navigate to the directory containing the file.
  4. Run the program with the following command:

    go run simple_chatbot.go
  5. Start chatting with the bot by typing your queries. Type “exit” to quit the program.

Happy Coding! If you have questions, feel free to reach out at support@learnprogramming.com.

Copyright © Learn Programming. All rights reserved.



By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)