A beginner-friendly command-line chatbot built with Go to demonstrate basic Go programming concepts and syntax.
This is a simple interactive chatbot that runs in your terminal. It's designed as a learning project for developers new to Go (Golang), showcasing fundamental concepts like:
- Package structure and imports
- Structs and methods
- String manipulation
- User input handling
- Control flow (switch statements, loops)
- Basic error handling
- Interactive Conversations: Responds to various user inputs
- Time & Date: Can tell you the current time and date
- Contextual Responses: Recognizes greetings, questions about itself, and Go-related topics
- Graceful Exit: Type 'quit', 'exit', 'bye', or 'goodbye' to end the conversation
- Help Command: Type 'help' to see available commands
- Error Handling: Handles empty inputs and unexpected errors
- Go 1.19+ installed on your system
- Text editor (VS Code recommended)
- Terminal/Command line access
# Install Go
sudo apt update
sudo rm -rf /usr/local/go
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
# Verify installation
go version-
Clone or create the project directory:
mkdir go-chatbot cd go-chatbot -
Initialize Go module:
go mod init chatbot
-
Create the main.go file and copy the source code
-
Run the chatbot:
go run main.go
go-chatbot/
โโโ README.md
โโโ go.mod
โโโ go.sum # (auto-generated)
โโโ main.go # Main application file
-
Start the chatbot:
go run main.go
-
Interact with the bot:
๐ค Hello! I'm GoBot v1.0 ๐ค I'm a simple chatbot built with Go! ๐ค Type 'quit' or 'exit' to end our conversation. ๐ค Let's chat! -------------------------------------------------- You: hello ๐ค Hello there! Nice to meet you! You: what's your name ๐ค I'm GoBot, your friendly Go chatbot! You: time ๐ค The current time is: 14:30:25 -
End the conversation:
You: quit ๐ค Goodbye! Thanks for chatting with me!
| Input | Response |
|---|---|
hello, hi |
Greeting response |
how are you |
Status check response |
what's your name, who are you |
Bot introduction |
time |
Current time |
date |
Current date |
go, golang |
Information about Go language |
help |
List of available commands |
quit, exit, bye, goodbye |
Exit the program |
This project teaches the following Go concepts:
package main // Entry point packageimport (
"bufio" // Buffered I/O
"fmt" // Formatting
"os" // Operating system interface
"strings" // String manipulation
"time" // Time functions
)type ChatBot struct {
name string
version string
}
func (bot *ChatBot) introduce() {
// Method implementation
}// Switch statements
switch {
case strings.Contains(input, "hello"):
return "Hello there!"
default:
return "Default response"
}
// For loops
for {
// Infinite loop with break conditions
}if err := scanner.Err(); err != nil {
fmt.Printf("Error reading input: %v\n", err)
}sudo snap install --classic code- Open VS Code
- Press
Ctrl+Shift+X - Search for "Go"
- Install the official Go extension by Google
- Press
Ctrl+Shift+P - Type "Go: Install/Update Tools"
- Install all recommended tools
go run main.go# Build for current platform
go build -o chatbot
# Run the executable
./chatbot# Build for Windows
GOOS=windows GOARCH=amd64 go build -o chatbot.exe
# Build for macOS
GOOS=darwin GOARCH=amd64 go build -o chatbot-mac
# Build for Linux
GOOS=linux GOARCH=amd64 go build -o chatbot-linuxExtend this chatbot by adding:
- More conversation topics and responses
- Persistent memory using files or databases
- Web interface using
net/httppackage - Natural Language Processing with external APIs
- Configuration files for responses
- Logging system for conversation history
- Unit tests for bot responses
- Command-line flags for different modes
- ChatBot Struct: Holds bot properties (name, version)
- introduce(): Bot's greeting method
- respond(): Core logic for processing user input
- shouldQuit(): Checks for exit commands
- main(): Program entry point with chat loop
- Struct methods with receivers
- String manipulation with
stringspackage - User input with
bufio.Scanner - Time formatting with
timepackage - Switch statements for pattern matching
- Slices for storing multiple responses
After mastering this project, consider learning:
- Web Development: Build a web-based chatbot with
net/http - Concurrency: Add goroutines for handling multiple users
- Databases: Store conversations in SQLite or PostgreSQL
- APIs: Integrate with external services
- Testing: Write unit tests with Go's testing package
- Deployment: Deploy to cloud platforms
This is a learning project! Feel free to:
- Add new conversation topics
- Improve response logic
- Add new features
- Fix bugs
- Enhance documentation
This project is open source and available under the MIT License.
- Your Name - Initial work and documentation
- Go team for creating an amazing language
- VS Code team for excellent Go support
- Ubuntu community for great development environment
Happy Coding! ๐
This project is part of learning Go programming language. Perfect for beginners who want to understand Go syntax through a practical, interactive example.