Table of Contents
- Introduction
- Prerequisites
- Setting Up the Project
- Creating the Password Data Structure
- Adding Passwords
- Retrieving Passwords
- Updating and Deleting Passwords
- Conclusion
Introduction
In this tutorial, we will create a command-line password manager using the Go programming language. This password manager will allow users to securely store, retrieve, update, and delete passwords through a simple command-line interface.
By the end of this tutorial, you will have a basic understanding of how to create a command-line application in Go, as well as how to work with file I/O for storing and retrieving data.
Prerequisites
To follow along with this tutorial, you should have the following:
- Basic knowledge of the Go programming language.
- Go installed on your machine.
- A code editor of your choice.
Setting Up the Project
-
Create a new directory for your project.
mkdir go-password-manager cd go-password-manager
-
Initialize a new Go module.
go mod init passwordmanager
-
Create a new Go file named
main.go
.touch main.go
-
Open
main.go
in your code editor.
Creating the Password Data Structure
Let’s start by creating a data structure to represent a password. Each password will have a title
and a value
. We will store multiple passwords in a slice.
-
Declare a struct to represent a password.
type Password struct { Title string Value string }
-
Declare a slice to store multiple passwords.
var passwords []Password
-
Add a function to display all the passwords.
func displayPasswords() { if len(passwords) == 0 { fmt.Println("No passwords found.") return } fmt.Println("Passwords:") for i, password := range passwords { fmt.Printf("%d. %s\n", i+1, password.Title) } }
Adding Passwords
Now, let’s implement the functionality to add passwords to our password manager.
-
Add a function to prompt the user for a new password and add it to the slice.
func addPassword() { fmt.Print("Enter the title: ") title := getInput() fmt.Print("Enter the password: ") value := getInput() password := Password{ Title: title, Value: value, } passwords = append(passwords, password) fmt.Println("Password added successfully!") }
-
Add a helper function to read input from the user.
func getInput() string { reader := bufio.NewReader(os.Stdin) input, _ := reader.ReadString('\n') return strings.TrimSpace(input) }
-
Update the
main
function to display a menu and handle user input.func main() { for { fmt.Println("[1] Add Password") fmt.Println("[2] Display Passwords") fmt.Println("[3] Exit") fmt.Print("Enter your choice: ") choice := getInput() switch choice { case "1": addPassword() case "2": displayPasswords() case "3": fmt.Println("Exiting...") os.Exit(0) default: fmt.Println("Invalid choice.") } } }
-
Run the program.
go run main.go
Retrieving Passwords
Next, let’s implement the functionality to retrieve passwords from our password manager.
-
Add a function to get a password by its title.
func getPassword(title string) (Password, bool) { for _, password := range passwords { if password.Title == title { return password, true } } return Password{}, false }
-
Modify the
displayPasswords
function to display the password value when selected.func displayPasswords() { if len(passwords) == 0 { fmt.Println("No passwords found.") return } fmt.Println("Passwords:") for i, password := range passwords { fmt.Printf("%d. %s\n", i+1, password.Title) } fmt.Print("Enter the number of the password to display: ") choice := getInput() index, _ := strconv.Atoi(choice) if index <= 0 || index > len(passwords) { fmt.Println("Invalid choice.") return } password := passwords[index-1] fmt.Println("Password:", password.Value) }
-
Run the program and test the retrieval of passwords.
go run main.go
Updating and Deleting Passwords
Finally, let’s add the functionality to update and delete passwords.
-
Add functions to update and delete a password.
func updatePassword(title string) { password, found := getPassword(title) if !found { fmt.Println("Password not found.") return } fmt.Print("Enter the new password: ") value := getInput() password.Value = value fmt.Println("Password updated successfully!") } func deletePassword(title string) { for i, password := range passwords { if password.Title == title { passwords = append(passwords[:i], passwords[i+1:]...) fmt.Println("Password deleted successfully!") return } } fmt.Println("Password not found.") }
-
Update the
main
function to handle update and delete operations.func main() { for { fmt.Println("[1] Add Password") fmt.Println("[2] Display Passwords") fmt.Println("[3] Update Password") fmt.Println("[4] Delete Password") fmt.Println("[5] Exit") fmt.Print("Enter your choice: ") choice := getInput() switch choice { case "1": addPassword() case "2": displayPasswords() case "3": fmt.Print("Enter the title of the password to update: ") title := getInput() updatePassword(title) case "4": fmt.Print("Enter the title of the password to delete: ") title := getInput() deletePassword(title) case "5": fmt.Println("Exiting...") os.Exit(0) default: fmt.Println("Invalid choice.") } } }
-
Run the program and test the update and delete operations.
go run main.go
Conclusion
In this tutorial, you have learned how to create a command-line password manager using Go. We covered the basic concepts of creating a Go program, working with file I/O for data storage, and implementing functionalities to add, retrieve, update, and delete passwords. You can further enhance this password manager by adding encryption and adding more advanced features. Happy coding!
This tutorial belongs to the categories: Syntax and Basics
, File I/O and System Interaction
.